I have the following situation: I want to compile a number of Scheme files with Gambit into an executable. To that end, I use gambit to translate/generate all Scheme files into C and object files, which in turn are then compiled and linked into an executable.
Assuming I have the following three files (example is taken from the Gambit documentation):
/* File: "m1.c" */
int power_of_2 (int x) { return 1<<x; }
; File: "m2.scm"
(c-declare "extern int power_of_2 ();")
(define pow2 (c-lambda (int) int "power_of_2"))
(define (twice x) (cons x x))
; File: "m3.scm"
(write (map twice (map pow2 '(1 2 3 4)))) (newline)
If I were to compile it by hand, this were the steps to take:
$ gsc -c m2.scm # create m2.c (note: .scm is optional)
$ gsc -c m3.scm # create m3.c (note: .scm is optional)
$ gsc -link m2.c m3.c # create the incremental link file m3_.c
$ gsc -obj m1.c m2.c m3.c m3_.c # create all object files *.o
m1.c:
m2.c:
m3.c:
m3_.c:
$ gcc m1.o m2.o m3.o m3_.o -lgambit -lm -ldl -lutil # link object files
$ ./a.out
((2 . 2) (4 . 4) (8 . 8) (16 . 16))
EDIT: Mind the last command, that's not a typo, the final step is to use gcc
for linking.
Now, I wish to use CMake to do this for me. Here's an outline of my CMakeLists.txt
:
cmake_minimum_required( VERSION 3.8...3.18 )
if( ${CMAKE_VERSION} VERSION_LESS 3.12 )
cmake_policy( VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} )
endif()
# Give this project a name
project( schemetemplate VERSION 0.0.1 )
# compile all .scm files into *.c files
# the C files as target
add_custom_target(
compiledSchemeFiles ALL
SOURCES m2.scm
m3.scm
COMMAND # this is where I call gsc(?)
VERBATIM )
# or do I use custom command?
add_custom_command( ... )
# the final executable, how to tell add_executable I want all generated files as dependency?
add_exectuable( finalexec m1.c ...)
I've never worked with CMake for anything but pure C/C++ project. What's the smartest way to go about this? Ultimately, I want so have a CMakeLists.txt
that I can place in a subdirectory with all my Scheme files, have it generate all C and object files, and have CMake in the parent directory use them to create executable and libraries with add_executable
and add_library
.