CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. cmake_minimum_required(VERSION 3.14)
  2. project(mcell)
  3. #TODO: unify case for cmake commands, e.g. set vs SET
  4. # Using default gcc 8.3.0 provided in Debian 10
  5. # causes a segfault in nfSIM, however different gcc is ok
  6. if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 8.3.0)
  7. message(FATAL_ERROR
  8. "There is an error in nfSim, possibly due to a compiler issue, with gcc-8.3.0. "
  9. "Use a different compiler with CC=... and CXX=... "
  10. )
  11. endif()
  12. # silence UseSWIG warnings
  13. if (${CMAKE_VERSION} VERSION_GREATER "3.13")
  14. cmake_policy(SET CMP0078 NEW)
  15. endif()
  16. if (${CMAKE_VERSION} VERSION_GREATER "3.13")
  17. cmake_policy(SET CMP0086 NEW)
  18. endif()
  19. add_definitions(-DPACKAGE_BUGREPORT="[email protected]")
  20. SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 ")
  21. SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 ")
  22. set(INCLUDE_DIRS " -isystem ${CMAKE_SOURCE_DIR}/libs/")
  23. if (CMAKE_BUILD_TYPE STREQUAL "Release")
  24. SET(OPTIMIZATION_FLAGS " -O3 -march=core2 -finline-limit=1000 ")
  25. # must not be used for pymcell (at least for now)
  26. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto ")
  27. endif()
  28. set(WARNING_FLAGS "-Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable")
  29. set(INCLUDE_DIRS " -isystem ${CMAKE_SOURCE_DIR}/libs/")
  30. SET(COMMON_C_CXX_FLAGS " ${WARNING_FLAGS} -g3 ${OPTIMIZATION_FLAGS} ${INCLUDE_DIRS} -D_GNU_SOURCE=1")
  31. SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_C_CXX_FLAGS} -std=c11 ")
  32. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_C_CXX_FLAGS} -std=c++14")
  33. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lm")
  34. if (CYGWIN)
  35. # cygwin app run in cygwin environment is somehow unable to load cygstdc++-6.dll
  36. # although it works fine when ran from 'cmd'
  37. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ")
  38. endif()
  39. option(USE_GCOV "Use gcc coverage" OFF)
  40. option(USE_PROFILING "Use gprof profiling" OFF)
  41. option(USE_LTO "Use link-time optimization" OFF)
  42. option(USE_SANITIZER "Use address sanitizer" OFF)
  43. option(PYMCELL "Build also pyMCell" ON)
  44. option(COMPILE_AS_CXX "Build MCell sources with a C++ compiler" OFF)
  45. if (USE_SANITIZER)
  46. SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address ")
  47. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address ")
  48. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lasan ")
  49. endif()
  50. if (USE_GCOV)
  51. SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage ")
  52. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage ")
  53. endif()
  54. if (USE_PROFILING)
  55. SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg ")
  56. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg ")
  57. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg ")
  58. endif()
  59. if (USE_LTO)
  60. SET(CMAKE_AR "gcc-ar")
  61. SET(CMAKE_LD "gcc-ld")
  62. SET(CMAKE_RANLIB "gcc-ranlib")
  63. SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto ")
  64. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto ")
  65. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto ")
  66. endif()
  67. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
  68. # directories holding flex/bison files and out of source includes.
  69. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/deps)
  70. include_directories(
  71. "${CMAKE_SOURCE_DIR}/src"
  72. "${CMAKE_CURRENT_BINARY_DIR}/deps"
  73. "${CMAKE_SOURCE_DIR}/../nfsimCInterface/src/"
  74. "${CMAKE_SOURCE_DIR}/include"
  75. )
  76. # move config.h into place. Unix and Windows have different header files
  77. if (UNIX)
  78. file(COPY ${CMAKE_SOURCE_DIR}/src/config-nix.h DESTINATION
  79. ${CMAKE_CURRENT_BINARY_DIR}/deps)
  80. file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/deps/config-nix.h
  81. ${CMAKE_CURRENT_BINARY_DIR}/deps/config.h)
  82. elseif (WIN32)
  83. file(COPY ${CMAKE_SOURCE_DIR}/src/config-win.h DESTINATION
  84. ${CMAKE_CURRENT_BINARY_DIR}/deps)
  85. file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/deps/config-win.h
  86. ${CMAKE_CURRENT_BINARY_DIR}/deps/config.h)
  87. endif()
  88. if (COMPILE_AS_CXX)
  89. SET(C_EXT "cpp")
  90. else()
  91. SET(C_EXT "c")
  92. endif()
  93. # check for bison/flex and set up code gen
  94. find_package(BISON)
  95. find_package(FLEX)
  96. BISON_TARGET(mdlParser ${CMAKE_SOURCE_DIR}/src/mdlparse.y
  97. ${CMAKE_CURRENT_BINARY_DIR}/deps/mdlparse.${C_EXT})
  98. BISON_TARGET(dynGeomParser ${CMAKE_SOURCE_DIR}/src/dyngeom_parse.y
  99. ${CMAKE_CURRENT_BINARY_DIR}/deps/dyngeom_yacc.${C_EXT})
  100. # not sure why is the lex file copied
  101. configure_file(${CMAKE_SOURCE_DIR}/src/mdllex.l ${CMAKE_CURRENT_BINARY_DIR}/deps/mdllex.l COPYONLY)
  102. FLEX_TARGET(mdlScanner ${CMAKE_CURRENT_BINARY_DIR}/deps/mdllex.l
  103. ${CMAKE_CURRENT_BINARY_DIR}/deps/mdlex.${C_EXT} COMPILE_FLAGS -Crema)
  104. ADD_FLEX_BISON_DEPENDENCY(mdlScanner mdlParser mdllex_l)
  105. configure_file(${CMAKE_SOURCE_DIR}/src/dyngeom_lex.l ${CMAKE_CURRENT_BINARY_DIR}/deps/dyngeom_lex.l COPYONLY)
  106. FLEX_TARGET(dynGeomScanner ${CMAKE_CURRENT_BINARY_DIR}/deps/dyngeom_lex.l
  107. ${CMAKE_CURRENT_BINARY_DIR}/deps/dyngeom_lex.${C_EXT} COMPILE_FLAGS -Crema)
  108. ADD_FLEX_BISON_DEPENDENCY(dynGeomScanner dynGeomParser dyngeomlex_l)
  109. # create version.h target
  110. if (NOT WIN32)
  111. add_custom_target(
  112. version_h
  113. COMMAND ${CMAKE_SOURCE_DIR}/src/version.sh > ${CMAKE_CURRENT_BINARY_DIR}/deps/version.h
  114. DEPENDS ${CMAKE_SOURCE_DIR}/src/version.sh
  115. VERBATIM)
  116. elseif (WIN32)
  117. add_custom_target(version_h)
  118. add_custom_command(TARGET version_h COMMAND ${CMAKE_COMMAND} -E
  119. copy ${CMAKE_SOURCE_DIR}/appveyor_windows/version.h ${CMAKE_CURRENT_BINARY_DIR}/deps )
  120. endif()
  121. add_subdirectory(${CMAKE_SOURCE_DIR}/../nfsim ${CMAKE_CURRENT_BINARY_DIR}/nfsim)
  122. add_subdirectory(${CMAKE_SOURCE_DIR}/../nfsimCInterface ${CMAKE_CURRENT_BINARY_DIR}/nfsimCInterface)
  123. set(SOURCE_FILES
  124. src/argparse.c
  125. src/chkpt.c
  126. src/count_util.c
  127. src/diffuse.c
  128. src/diffuse_trimol.c
  129. src/diffuse_util.c
  130. src/dyngeom.c
  131. src/dyngeom_parse_extras.c
  132. src/grid_util.c
  133. src/map_c.cpp
  134. src/init.c
  135. src/isaac64.c
  136. src/logging.c
  137. src/mcell_dyngeom.c
  138. src/mcell_init.c
  139. src/mcell_misc.c
  140. src/mcell_objects.c
  141. src/mcell_react_out.c
  142. src/mcell_reactions.c
  143. src/mcell_release.c
  144. src/mcell_run.c
  145. src/mcell_species.c
  146. src/mcell_surfclass.c
  147. src/mcell_viz.c
  148. src/mem_util.c
  149. src/minrng.c
  150. src/nfsim_func.c
  151. src/react_cond.c
  152. src/react_outc.c
  153. src/react_outc_nfsim.c
  154. src/react_outc_trimol.c
  155. src/react_output.c
  156. src/react_trig.c
  157. src/react_trig_nfsim.c
  158. src/react_util.c
  159. src/react_util_nfsim.c
  160. src/rng.c
  161. src/sched_util.c
  162. src/strfunc.c
  163. src/sym_table.c
  164. src/test_api.c
  165. src/triangle_overlap.c
  166. src/util.c
  167. src/vector.c
  168. src/version_info.c
  169. src/viz_output.c
  170. src/vol_util.c
  171. src/volume_output.c
  172. src/wall_util.c
  173. )
  174. set(SOURCE_FILES_ONLY_MCELL
  175. src/mdlparse_util.c
  176. src/mcell.c
  177. )
  178. if (COMPILE_AS_CXX)
  179. SET_SOURCE_FILES_PROPERTIES( ${SOURCE_FILES} PROPERTIES LANGUAGE CXX )
  180. SET_SOURCE_FILES_PROPERTIES( ${SOURCE_FILES_ONLY_MCELL} PROPERTIES LANGUAGE CXX )
  181. endif()
  182. link_directories(${CMAKE_BINARY_DIR}/lib)
  183. if (UNIX)
  184. SET(CMAKE_INSTALL_RPATH "$ORIGIN/./lib")
  185. SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  186. endif()
  187. FIND_PACKAGE(SWIG REQUIRED)
  188. INCLUDE(${SWIG_USE_FILE})
  189. INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/src")
  190. if (NOT WIN32 AND NOT CYGWIN)
  191. FIND_PACKAGE(PythonLibs 3 REQUIRED)
  192. INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
  193. #SET(CMAKE_SWIG_FLAGS "-debug-typemap;-debug-tmsearch;-debug-tmused;-ltypemaps")
  194. SET(CMAKE_SWIG_FLAGS "-ltypemaps;-py3")
  195. if (COMPILE_AS_CXX)
  196. SET(CMAKE_SWIG_FLAGS "-c++;${CMAKE_SWIG_FLAGS}")
  197. endif()
  198. SET(CMAKE_SWIG_OUTDIR "${CMAKE_BINARY_DIR}/python")
  199. SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python")
  200. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python)
  201. if (COMPILE_AS_CXX)
  202. SET_SOURCE_FILES_PROPERTIES(src/pymcell.i PROPERTIES CPLUSPLUS ON)
  203. else()
  204. SET_SOURCE_FILES_PROPERTIES(src/pymcell.i PROPERTIES C ON)
  205. endif()
  206. SET_SOURCE_FILES_PROPERTIES(src/pymcell.i PROPERTIES SWIG_FLAGS "-includeall")
  207. if (${CMAKE_VERSION} VERSION_GREATER "3.8")
  208. SWIG_ADD_LIBRARY(pymcell
  209. TYPE SHARED
  210. LANGUAGE python
  211. SOURCES src/pymcell.i
  212. ${SOURCE_FILES}
  213. ${BISON_dynGeomParser_OUTPUTS}
  214. ${FLEX_dynGeomScanner_OUTPUTS}
  215. )
  216. else()
  217. SWIG_ADD_MODULE(pymcell python
  218. src/pymcell.i
  219. ${SOURCE_FILES}
  220. ${BISON_dynGeomParser_OUTPUTS}
  221. ${FLEX_dynGeomScanner_OUTPUTS}
  222. )
  223. endif()
  224. if (APPLE)
  225. SWIG_LINK_LIBRARIES(pymcell ${CMAKE_CURRENT_BINARY_DIR}/lib/libnfsim_c.dylib ${CMAKE_CURRENT_BINARY_DIR}/lib/libNFsim.dylib ${PYTHON_LIBRARIES})
  226. else()
  227. SWIG_LINK_LIBRARIES(pymcell ${CMAKE_CURRENT_BINARY_DIR}/lib/libnfsim_c.so ${CMAKE_CURRENT_BINARY_DIR}/lib/libNFsim.so ${PYTHON_LIBRARIES})
  228. endif()
  229. # copy the pyMCell test scripts into place
  230. file(GLOB PYMCELL_EXAMPLES "${CMAKE_SOURCE_DIR}/src/pymcell_examples/*")
  231. file(COPY ${PYMCELL_EXAMPLES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python/)
  232. # configure_file copies file even for subsequent runs of make
  233. configure_file(${CMAKE_SOURCE_DIR}/src/data_model_import.py ${CMAKE_CURRENT_BINARY_DIR}/python/data_model_import.py COPYONLY)
  234. configure_file(${CMAKE_SOURCE_DIR}/src/pymcell_unittests.py ${CMAKE_CURRENT_BINARY_DIR}/python/pymcell_unittests.py COPYONLY)
  235. configure_file(${CMAKE_SOURCE_DIR}/src/pymcell_helpers.py ${CMAKE_CURRENT_BINARY_DIR}/python/pymcell_helpers.py COPYONLY)
  236. endif()
  237. configure_file(${CMAKE_SOURCE_DIR}/src/pymcell_helpers.py ${CMAKE_CURRENT_BINARY_DIR}/python/pymcell_helpers.py COPYONLY)
  238. # copy tools for MDLr to MDL
  239. # TODO: list all files using configure_file
  240. configure_file(${CMAKE_SOURCE_DIR}/src/rules_py/nfsim_python.py ${CMAKE_CURRENT_BINARY_DIR}/nfsim_python.py COPYONLY)
  241. configure_file(${CMAKE_SOURCE_DIR}/src/rules_py/mdlr2mdl.py ${CMAKE_CURRENT_BINARY_DIR}/mdlr2mdl.py COPYONLY)
  242. file(COPY ${CMAKE_SOURCE_DIR}/src/rules_py/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
  243. # copy tools for MDLr to MDL
  244. file(COPY ${CMAKE_SOURCE_DIR}/../bionetgen/bng2/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bng2/)
  245. if (COMPILE_AS_CXX)
  246. SET_SOURCE_FILES_PROPERTIES( ${SOURCE_FILES} PROPERTIES LANGUAGE CXX )
  247. endif()
  248. # build executable
  249. add_executable(mcell
  250. ${SOURCE_FILES}
  251. ${SOURCE_FILES_ONLY_MCELL}
  252. ${BISON_mdlParser_OUTPUTS}
  253. ${BISON_dynGeomParser_OUTPUTS}
  254. ${FLEX_mdlScanner_OUTPUTS}
  255. ${FLEX_dynGeomScanner_OUTPUTS}
  256. )
  257. add_dependencies(mcell version_h)
  258. if (NOT WIN32 AND NOT CYGWIN)
  259. add_dependencies(pymcell version_h nfsim_c NFsim)
  260. endif()
  261. target_link_libraries(mcell nfsim_c_static NFsim_static)
  262. TARGET_COMPILE_DEFINITIONS(mcell PRIVATE NOSWIG=1)