diff --git a/doc/sphinx/cxx-guide/compiling.rst b/doc/sphinx/cxx-guide/compiling.rst index a995a69e3..e461b4ceb 100644 --- a/doc/sphinx/cxx-guide/compiling.rst +++ b/doc/sphinx/cxx-guide/compiling.rst @@ -74,6 +74,36 @@ contained in the ``samples`` subdirectory of the Cantera installation directory. For more information on SCons, see the `SCons Wiki `_ and the `SCons homepage `_. +CMake +===== + +CMake is a multi-platform build system which uses a high-level project +description to generate platform-specific build scripts (i.e. on Linux, CMake +will generate Makefiles). The configuration file for a CMake project is called +``CMakeLists.txt``. A typical ``CMakeLists.txt`` file for compiling a program +that uses Cantera might look like this: + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.1) + project (sample) + + set(CMAKE_VERBOSE_MAKEFILE ON) + set(CMAKE_CXX_STANDARD 11) + + find_package(Threads REQUIRED) + + include_directories("/opt/cantera/include" "/opt/sundials-2.7.0/include") + link_directories("/opt/cantera/lib" "/opt/sundials-2.7.0/lib") + + add_executable(sample sample.cpp) + target_link_libraries(sample cantera sundials_cvodes sundials_ida sundials_nvecserial fmt Threads::Threads) + +Several example ``CMakeLists.txt`` files are included with the C++ examples +contained in the ``samples`` subdirectory of the Cantera installation directory, +which have the paths and lists of libraries correctly configured for system on +which they are installed. + Make ==== diff --git a/samples/cxx/CMakeLists.txt.in b/samples/cxx/CMakeLists.txt.in new file mode 100644 index 000000000..353f591b2 --- /dev/null +++ b/samples/cxx/CMakeLists.txt.in @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.1) +project (@tmpl_progname@) + +set(CMAKE_VERBOSE_MAKEFILE ON) +set(CMAKE_CXX_STANDARD 11) + +find_package(Threads REQUIRED) + +include_directories(@cmake_cantera_incdirs@) +link_directories(@cmake_cantera_libdirs@) + +add_executable(@tmpl_progname@ @tmpl_sourcename@) +target_link_libraries(@tmpl_progname@ @cmake_cantera_libs@ Threads::Threads) diff --git a/samples/cxx/SConscript b/samples/cxx/SConscript index 7a0fa5c47..505d7a47d 100644 --- a/samples/cxx/SConscript +++ b/samples/cxx/SConscript @@ -29,8 +29,11 @@ for subdir, name, extensions in samples: localenv['tmpl_compiler_flags'] = repr(localenv['CCFLAGS'] + localenv['CXXFLAGS']) localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS']) localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x]) + localenv['cmake_cantera_incdirs'] = ' '.join(quoted(x) for x in incdirs if x) localenv['tmpl_cantera_libs'] = repr(localenv['cantera_libs']) + localenv['cmake_cantera_libs'] = ' '.join(localenv['cantera_libs']) localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x]) + localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x) localenv['tmpl_cantera_linkflags'] = repr(localenv['LINKFLAGS']) localenv['tmpl_progname'] = name localenv['tmpl_sourcename'] = name + '.cpp' @@ -46,6 +49,10 @@ for subdir, name, extensions in samples: sconstruct = localenv.SubstFile(pjoin(subdir, 'SConstruct'), 'SConstruct.in') install(pjoin('$inst_sampledir', 'cxx', subdir), sconstruct) + ## Generate CMakeList.txt files to be installed + cmakelists = localenv.SubstFile(pjoin(subdir, 'CMakeLists.txt'), 'CMakeLists.txt.in') + install(pjoin('$inst_sampledir', 'cxx', subdir), cmakelists) + ## Generate Makefiles to be installed mak_path = pjoin(localenv['ct_incroot'], 'cantera', 'Cantera.mak') localenv['mak_compiler_flags'] = ' '.join(localenv['CCFLAGS'] + localenv['CXXFLAGS']) diff --git a/samples/f90/CMakeLists.txt.in b/samples/f90/CMakeLists.txt.in new file mode 100644 index 000000000..4373e0cf9 --- /dev/null +++ b/samples/f90/CMakeLists.txt.in @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.1) +project (@tmpl_progname@) + +set(CMAKE_VERBOSE_MAKEFILE ON) +enable_language(Fortran) + +find_package(Threads REQUIRED) + +include_directories(@cmake_cantera_incdirs@) +link_directories(@cmake_cantera_libdirs@) + +add_executable(@tmpl_progname@ @tmpl_sourcename@) +target_link_libraries(@tmpl_progname@ @cmake_cantera_libs@ Threads::Threads) diff --git a/samples/f90/SConscript b/samples/f90/SConscript index ce498d88c..c5cf74ad6 100644 --- a/samples/f90/SConscript +++ b/samples/f90/SConscript @@ -44,6 +44,12 @@ for programName, sources in samples: sconstruct = localenv.SubstFile('SConstruct', 'SConstruct.in') + # Generate CMakeLists.txt to be installed + localenv['cmake_cantera_incdirs'] = ' '.join(quoted(x) for x in incdirs if x) + localenv['cmake_cantera_libs'] = ' '.join(libs) + localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x) + cmakelists = localenv.SubstFile('CMakeLists.txt', 'CMakeLists.txt.in') + # Generate Makefile to be installed localenv['make_target'] = programName localenv['make_sourcefile'] = programName + '.f90' @@ -51,3 +57,4 @@ for programName, sources in samples: install('$inst_sampledir/f90', makefile) install('$inst_sampledir/f90', sconstruct) + install('$inst_sampledir/f90', cmakelists)