1 set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
2 set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
3 cmake_minimum_required(VERSION 2.8.12...4.0)
4 include(cmake/CMakeModulePath.cmake)
5 include(cmake/Languages.cmake)
6 project(liba ${LANGUAGES})
7 include(cmake/Project.cmake)
8 include(cmake/core/file.cmake)
9 include(cmake/core/string.cmake)
10 include(cmake/CMakeByteOrder.cmake)
11 include(cmake/CMakeBuildType.cmake)
12 include(cmake/EnabledLanguages.cmake)
13 include(cmake/TargetSupportsSharedLibs.cmake)
14 include(cmake/TargetSupportsExecutables.cmake)
15 include(cmake/TargetSupportsSymbolicLink.cmake)
17 option(LIBA_STATIC "Enable/Disable finding and linking static libraries" 0)
20 include(cmake/CMakeFindLibraryStatic.cmake)
23 option(LIBA_WARNINGS "Enable/Disable warnings" 0)
26 include(cmake/TargetWarnings.cmake)
29 option(LIBA_ANALYZER "Enable/Disable analyzer" 0)
32 include(cmake/TargetAnalyzer.cmake)
35 option(LIBA_SANITIZE "Enable/Disable sanitize" 0)
38 include(cmake/TargetSanitize.cmake)
42 include(cmake/TargetStatic.cmake)
45 option(LIBA_CCACHE "Enable/Disable ccache" 0)
48 include(cmake/UseCCache.cmake)
52 set(LIBA_MATH "m" CACHE FILEPATH "system math library")
54 set(LIBA_MATH "" CACHE FILEPATH "system math library")
57 set(LIBA_INSTALL "" CACHE STRING "Specify installation behavior")
58 set(LIBA_FLOAT 8 CACHE STRING "floating-point number bytes")
59 set_property(CACHE LIBA_FLOAT PROPERTY STRINGS 4 8 16)
60 include(CheckLibraryExists)
62 function(check_math VARIABLE FUNCTION)
63 get_filename_component(LOCATION "${LIBA_MATH}" DIRECTORY)
64 list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIBA_MATH})
66 if(LIBA_FLOAT GREATER 8)
67 set(FUNCTION ${FUNCTION}l)
68 elseif(LIBA_FLOAT EQUAL 4)
69 set(FUNCTION ${FUNCTION}f)
72 check_library_exists("${LIBA_MATH}"
73 ${FUNCTION} "${LOCATION}" ${VARIABLE}
77 check_math(A_HAVE_EXPM1 expm1)
78 check_math(A_HAVE_LOG1P log1p)
79 check_math(A_HAVE_HYPOT hypot)
80 check_math(A_HAVE_ATAN2 atan2)
81 check_math(A_HAVE_CSQRT csqrt)
82 check_math(A_HAVE_CPOW cpow)
83 check_math(A_HAVE_CEXP cexp)
84 check_math(A_HAVE_CLOG clog)
85 check_math(A_HAVE_CSIN csin)
86 check_math(A_HAVE_CCOS ccos)
87 check_math(A_HAVE_CTAN ctan)
88 check_math(A_HAVE_CSINH csinh)
89 check_math(A_HAVE_CCOSH ccosh)
90 check_math(A_HAVE_CTANH ctanh)
91 check_math(A_HAVE_CASIN casin)
92 check_math(A_HAVE_CACOS cacos)
93 check_math(A_HAVE_CATAN catan)
94 check_math(A_HAVE_CASINH casinh)
95 check_math(A_HAVE_CACOSH cacosh)
96 check_math(A_HAVE_CATANH catanh)
98 option(LIBA_DOXYGEN "Enable/Disable doxygen" 0)
101 find_package(Doxygen OPTIONAL_COMPONENTS dot mscgen dia)
104 option(LIBA_CPPCHECK "Enable/Disable cppcheck" 0)
107 find_package(Cppcheck)
110 option(LIBA_CLANG_TIDY "Enable/Disable clang-tidy" 0)
113 find_package(ClangTidy)
116 option(LIBA_IWYU "Enable/Disable include-what-you-use" 0)
122 option(LIBA_IPO "Enable/Disable interprocedural optimization" 0)
124 # https://cmake.org/cmake/help/latest/module/CheckIPOSupported.html
125 if(NOT CMAKE_VERSION VERSION_LESS 3.9 AND LIBA_IPO)
126 cmake_policy(SET CMP0069 NEW)
127 include(CheckIPOSupported)
128 check_ipo_supported(RESULT LIBA_IPO)
131 if(DEFINED ENV{ANDROID_ROOT} AND EXISTS $ENV{ANDROID_ROOT})
132 option(LIBA_PIE "Enable/Disable position independent code" 1)
134 option(LIBA_PIE "Enable/Disable position independent code" 0)
137 # https://cmake.org/cmake/help/latest/module/CheckPIESupported.html
138 if(NOT CMAKE_VERSION VERSION_LESS 3.14)
139 cmake_policy(SET CMP0083 NEW)
140 include(CheckPIESupported)
141 check_pie_supported()
144 function(set_library_options)
145 if(LIBA_IWYU AND IWYU_FOUND)
146 add_include_what_you_use(TARGETS ${ARGN})
149 if(LIBA_CLANG_TIDY AND CLANG_TIDY_FOUND)
150 add_clang_tidy(TARGETS ${ARGN} OPTIONS --fix)
153 if(LIBA_CPPCHECK AND CPPCHECK_FOUND)
154 add_cppcheck(TARGETS ${ARGN} OPTIONS --enable=warning,performance)
157 set_target_properties(${ARGN} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${LIBA_IPO}
158 C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1
161 if(CMAKE_C_COMPILER_ID MATCHES "TinyCC" AND CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
162 set_property(TARGET ${ARGN} PROPERTY IMPORT_SUFFIX .def)
166 target_compile_warnings(${ARGN})
170 target_compile_analyzer(${ARGN})
174 function(add_library_properties target scope source)
175 target_compile_definitions(${target} ${scope} $<TARGET_PROPERTY:${source},${ARGV3}COMPILE_DEFINITIONS>)
176 target_include_directories(${target} ${scope} $<TARGET_PROPERTY:${source},${ARGV3}INCLUDE_DIRECTORIES>)
178 if(${scope} MATCHES "PUBLIC|PRIVATE")
179 set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES $<TARGET_PROPERTY:${source},${ARGV3}LINK_LIBRARIES>)
182 if(${scope} MATCHES "PUBLIC|INTERFACE")
183 set_property(TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<TARGET_PROPERTY:${source},${ARGV3}LINK_LIBRARIES>)
187 option(LIBA_MIMALLOC "Enable/Disable override using mimalloc" 0)
190 find_package(mimalloc)
194 if(TARGET mimalloc-static)
195 set(MIMALLOC_LIBRARY mimalloc-static)
197 set(MIMALLOC_LIBRARY mimalloc)
201 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git)
205 function(git_command output)
206 execute_process(ERROR_QUIET
207 COMMAND ${GIT_EXECUTABLE} ${ARGN}
208 OUTPUT_STRIP_TRAILING_WHITESPACE
209 OUTPUT_VARIABLE ${output}
211 set(${output} ${${output}} PARENT_SCOPE)
214 git_command(GIT_COMMIT_LONG log -1 --pretty=format:%H)
215 git_command(GIT_COMMIT_DATE log -1 --pretty=format:%cd --date=format:%Y%m%d)
219 set(${PROJECT_NAME}_VERSION_TWEAK ${GIT_COMMIT_DATE})
220 set(PROJECT_VERSION_TWEAK ${GIT_COMMIT_DATE})
221 set(A_VERSION_TWEAK ${GIT_COMMIT_DATE})
225 if(NOT EXISTS "${A_HAVE_H}")
226 set(A_HAVE_H ${CMAKE_CURRENT_BINARY_DIR}/a.cmake.h)
227 configure_file(${CMAKE_CURRENT_LIST_DIR}/include/a.cmake.h.in ${A_HAVE_H})
230 if(IS_ABSOLUTE "${LIBA_MATH}")
231 get_filename_component(math ${LIBA_MATH} NAME_WE)
232 string(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}(.+)" "\\1" math "${math}")
233 set(LIBA_MATH $<BUILD_INTERFACE:${LIBA_MATH}> $<INSTALL_INTERFACE:${math}>)
236 if(TARGET_SUPPORTS_SYMBOLIC_LINK)
237 option(LIBA_SYMLINK "Enable/Disable symbolic link" 1)
240 function(set_library_compile target)
241 if(EXISTS "${A_HAVE_H}")
242 file(RELATIVE_PATH a_have_h ${PROJECT_SOURCE_DIR}/include/a ${A_HAVE_H})
243 target_compile_definitions(${target} PUBLIC $<BUILD_INTERFACE:A_HAVE_H="${a_have_h}">)
246 if((LIBA_SYMLINK OR WIN32) AND TARGET_SUPPORTS_SYMBOLIC_LINK)
247 set_target_properties(${target} PROPERTIES SOVERSION
248 ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION}
252 set_target_properties(${target} PROPERTIES OUTPUT_NAME a
253 POSITION_INDEPENDENT_CODE 1 DEFINE_SYMBOL A_EXPORTS
255 target_compile_definitions(${target} PRIVATE A_EXPORTS)
256 target_include_directories(${target} BEFORE PUBLIC
257 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
258 $<INSTALL_INTERFACE:include>
260 get_property(TYPE TARGET ${target} PROPERTY TYPE)
262 if(${TYPE} MATCHES "(STATIC|SHARED|MODULE)_LIBRARY")
263 target_link_libraries(${target} PUBLIC ${LIBA_MATH})
266 set_library_options(${target})
270 file_scaner(SOURCES RECURSE include src EXT c h cc hh cpp hpp)
272 file_scaner(SOURCES RECURSE include src EXT c h)
275 add_library(a OBJECT ${SOURCES})
276 set_library_compile(a)
278 if(LIBA_MIMALLOC AND mimalloc_FOUND)
279 set_property(SOURCE ${CMAKE_CURRENT_LIST_DIR}/src/a.c
280 APPEND PROPERTY COMPILE_DEFINITIONS A_HAVE_MIMALLOC_H
283 if(CMAKE_VERSION VERSION_LESS 3.12)
284 add_library_properties(a PUBLIC ${MIMALLOC_LIBRARY} INTERFACE_)
286 target_link_libraries(a PUBLIC ${MIMALLOC_LIBRARY})
290 include(GNUInstallDirs)
291 set(INSTALL_TARGETS "")
293 if("${LIBA_INSTALL}" MATCHES "^$|static")
294 list(APPEND INSTALL_TARGETS alib)
297 add_library(alib STATIC $<TARGET_OBJECTS:a>)
298 set_library_compile(alib)
300 if(LIBA_MIMALLOC AND mimalloc_FOUND)
301 target_link_libraries(alib PUBLIC ${MIMALLOC_LIBRARY})
304 if(TARGET_SUPPORTS_SHARED_LIBS)
305 if("${LIBA_INSTALL}" MATCHES "^$|shared")
306 list(APPEND INSTALL_TARGETS liba)
309 add_library(liba SHARED $<TARGET_OBJECTS:a>)
310 set_library_compile(liba)
312 if(LIBA_MIMALLOC AND mimalloc_FOUND)
313 target_link_libraries(liba PUBLIC ${MIMALLOC_LIBRARY})
316 if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
317 set_property(TARGET liba PROPERTY OUTPUT_NAME liba)
320 target_compile_definitions(liba INTERFACE A_IMPORTS)
323 target_link_static(liba)
328 add_library(asan OBJECT ${SOURCES})
329 set_library_compile(asan)
330 target_compile_sanitize(asan)
332 if(LIBA_MIMALLOC AND mimalloc_FOUND)
333 if(CMAKE_VERSION VERSION_LESS 3.12)
334 add_library_properties(asan PUBLIC ${MIMALLOC_LIBRARY} INTERFACE_)
336 target_link_libraries(asan PUBLIC ${MIMALLOC_LIBRARY})
342 include(cmake/InstallTargets.cmake)
343 install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/a DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
345 if(EXISTS "${A_HAVE_H}")
346 install(FILES ${A_HAVE_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/a)
347 install_hook(${CMAKE_INSTALL_INCLUDEDIR}/a/a.h A_HAVE_H ${A_HAVE_H})
351 option(LIBA_PKGCONFIG "Enable/Disable pkgconfig" 0)
354 include(cmake/InstallPkgConfig.cmake)
355 install_pkgconfig(${PROJECT_NAME} alib)
358 option(LIBA_LIBTOOL "Enable/Disable libtool" 0)
360 if(LIBA_LIBTOOL AND INSTALL_TARGETS)
361 include(cmake/InstallLibtool.cmake)
362 install_libtool(${INSTALL_TARGETS})
365 set(LIBA_VCPKG "" CACHE PATH "vcpkg installation root")
367 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/vcpkg AND LIBA_VCPKG AND GIT_COMMIT_LONG)
368 if(NOT EXISTS ${LIBA_VCPKG})
369 set(LIBA_VCPKG ${CMAKE_CURRENT_BINARY_DIR}/vcpkg)
370 install(DIRECTORY ${LIBA_VCPKG} DESTINATION ${CMAKE_INSTALL_DATADIR})
374 ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/portfile.cmake
375 ${LIBA_VCPKG}/ports/${PROJECT_NAME}/portfile.cmake
379 ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/vcpkg.json
380 ${LIBA_VCPKG}/ports/${PROJECT_NAME}/vcpkg.json
385 if(PROJECT_IS_TOP_LEVEL)
386 add_custom_target(afmt)
387 add_custom_target(adoc)
391 if(NOT CMAKE_FIND_LIBRARY_PREFIXES)
392 set(CMAKE_FIND_LIBRARY_PREFIXES ";lib")
395 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/java/CMakeLists.txt)
396 option(LIBA_JAVA "Enable/Disable java" 0)
403 if(LIBA_JAVA AND Java_JAVAC_EXECUTABLE)
404 add_subdirectory(java)
408 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/javascript/CMakeLists.txt)
410 option(LIBA_JAVASCRIPT "Enable/Disable javascript" 1)
412 option(LIBA_JAVASCRIPT "Enable/Disable javascript" 0)
417 find_package(Emscripten)
420 if(EMSCRIPTEN OR EMSCRIPTEN_FOUND)
421 add_subdirectory(javascript)
426 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/lua/CMakeLists.txt)
427 option(LIBA_LUA "Enable/Disable lua" 0)
428 option(LIBA_LUAJIT "Enable/Disable luajit" 0)
429 option(LIBA_WITH_LUA "Enable/Disable build with lua" 0)
431 if(LIBA_LUA OR LIBA_LUAJIT OR LIBA_WITH_LUA)
432 set(LUA_MATH_LIBRARY ${LIBA_MATH} CACHE STRING "lua math library")
435 if(NOT LIBA_WITH_LUA AND LIBA_LUA)
436 find_package(Lua OPTIONAL_COMPONENTS lua)
437 set(LUA_LIBRARIES lualib)
440 if((LIBA_LUA AND NOT LUA_FOUND AND NOT LIBA_WITH_LUA) OR LIBA_LUAJIT)
441 find_package(LuaJIT OPTIONAL_COMPONENTS luajit)
442 set(LUA_EXECUTABLE ${LUAJIT_EXECUTABLE})
443 set(LUA_LIBRARIES luajit)
446 if(LIBA_LUA AND LUA_FOUND)
447 add_subdirectory(lua)
448 elseif(LIBA_LUAJIT AND LUAJIT_FOUND)
449 add_subdirectory(lua)
450 elseif(LIBA_WITH_LUA)
451 add_subdirectory(lua)
455 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/python/CMakeLists.txt)
456 option(LIBA_PYTHON "Enable/Disable python" 0)
459 find_package(Python COMPONENTS Development OPTIONAL_COMPONENTS Interpreter)
463 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/python/src/liba.c OR CYTHON_FOUND)
465 add_subdirectory(python)
470 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/quickjs/CMakeLists.txt)
471 option(LIBA_QUICKJS "Enable/Disable quickjs" 0)
472 option(LIBA_WITH_QUICKJS "Enable/Disable build with quickjs" 0)
474 if(LIBA_QUICKJS OR LIBA_WITH_QUICKJS)
475 set(QUICKJS_MATH_LIBRARY ${LIBA_MATH} CACHE STRING "quickjs math library")
478 if(NOT LIBA_WITH_QUICKJS AND LIBA_QUICKJS)
479 find_package(QuickJS)
482 if(LIBA_QUICKJS AND QUICKJS_FOUND)
483 add_subdirectory(quickjs)
484 elseif(LIBA_WITH_QUICKJS)
485 add_subdirectory(quickjs)
489 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/test/CMakeLists.txt)
490 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
491 add_subdirectory(test)
495 set(CPACK_SOURCE_IGNORE_FILES ${CMAKE_CURRENT_LIST_DIR}/.git)
497 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git AND GIT_FOUND)
498 execute_process(ERROR_QUIET
499 COMMAND ${GIT_EXECUTABLE} status -z --ignored=matching
500 OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE ignore
502 string(REPLACE "." "\\\\." ignore "${ignore}")
503 string(REPLACE "*" ".*" ignore "${ignore}")
504 string(REPLACE "?? " "!! " ignore "${ignore}")
505 string(REGEX MATCHALL "!! [^!]+" ignore "${ignore}")
506 string(REPLACE "!! " "${CMAKE_CURRENT_LIST_DIR}/" ignore "${ignore}")
507 list(APPEND CPACK_SOURCE_IGNORE_FILES ${ignore})
510 set(CPACK_RESOURCE_FILE_README ${CMAKE_CURRENT_LIST_DIR}/README.md)
511 set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_LIST_DIR}/LICENSE.txt)
514 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
515 find_package(ClangFormat)
518 if(CLANG_FORMAT_FOUND)
519 file_scaner(SOURCES EXT c h cc hh cpp hpp RECURSE include src test
520 java/src javascript/src lua/src quickjs/src
522 add_clang_format(afmt.c VERBOSE ${SOURCES})
523 add_dependencies(afmt afmt.c)
526 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
531 add_black(afmt.py ${CMAKE_CURRENT_LIST_DIR})
532 add_dependencies(afmt afmt.py)
535 if(NOT CMAKE_VERSION VERSION_LESS 3.9 AND DOXYGEN_FOUND)
536 # Project related configuration options
537 set(DOXYGEN_CREATE_SUBDIRS YES)
538 set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
540 # Build related configuration options
541 set(DOXYGEN_EXTRACT_STATIC YES)
543 # Configuration options related to warning and progress messages
544 set(DOXYGEN_WARN_IF_UNDOCUMENTED NO)
545 set(DOXYGEN_WARN_NO_PARAMDOC YES)
547 # Configuration options related to the input files
548 set(DOXYGEN_RECURSIVE YES)
550 # Configuration options related to source browsing
551 # Configuration options related to the alphabetical class index
552 # Configuration options related to the HTML output
553 set(DOXYGEN_GENERATE_HTML YES)
554 set(DOXYGEN_USE_MATHJAX YES)
555 set(DOXYGEN_MATHJAX_VERSION MathJax_3)
557 # Configuration options related to the LaTeX output
558 # Configuration options related to the RTF output
559 # Configuration options related to the man page output
560 # Configuration options related to the XML output
561 set(DOXYGEN_GENERATE_XML YES)
563 # Configuration options related to the DOCBOOK output
564 # Configuration options for the AutoGen Definitions output
565 # Configuration options related to Sqlite3 output
566 # Configuration options related to the Perl module output
567 # Configuration options related to the preprocessor
568 set(DOXYGEN_MACRO_EXPANSION YES)
569 set(DOXYGEN_PREDEFINED
570 "__STDC_VERSION__=201112L"
571 "__cplusplus=201103L"
583 file(STRINGS "${A_HAVE_H}" macros REGEX "#define[ ]+A_HAVE_.+")
585 foreach(macro ${macros})
586 string(REGEX REPLACE "#define[ ]+(.*)[ ]+(.+)" "\\1=\\2" macro "${macro}")
587 list(APPEND DOXYGEN_PREDEFINED ${macro})
590 # Configuration options related to external references
591 # Configuration options related to the dot tool
592 if(Doxygen_dot_FOUND)
593 set(DOXYGEN_HAVE_DOT YES)
594 set(DOXYGEN_INTERACTIVE_SVG YES)
595 set(DOXYGEN_DOT_IMAGE_FORMAT svg)
596 set(DOXYGEN_DOT_MULTI_TARGETS YES)
599 # https://www.doxygen.nl/manual/config.html
600 doxygen_add_docs(adoc.c WORKING_DIRECTORY
601 ${CMAKE_CURRENT_LIST_DIR}/include/a
602 ${CMAKE_CURRENT_LIST_DIR}/include/a
603 ${CMAKE_CURRENT_LIST_DIR}/README.md
605 add_dependencies(adoc adoc.c)