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_PYTHON "Enable/Disable python" 0)
101 find_package(Python COMPONENTS Interpreter OPTIONAL_COMPONENTS Development)
105 option(LIBA_DOXYGEN "Enable/Disable doxygen" 0)
108 find_package(Doxygen OPTIONAL_COMPONENTS dot mscgen dia)
111 option(LIBA_CPPCHECK "Enable/Disable cppcheck" 0)
114 find_package(Cppcheck)
117 option(LIBA_CLANG_TIDY "Enable/Disable clang-tidy" 0)
120 find_package(ClangTidy)
123 option(LIBA_IWYU "Enable/Disable include-what-you-use" 0)
129 option(LIBA_IPO "Enable/Disable interprocedural optimization" 0)
131 # https://cmake.org/cmake/help/latest/module/CheckIPOSupported.html
132 if(NOT CMAKE_VERSION VERSION_LESS 3.9 AND LIBA_IPO)
133 cmake_policy(SET CMP0069 NEW)
134 include(CheckIPOSupported)
135 check_ipo_supported(RESULT LIBA_IPO)
138 if(DEFINED ENV{ANDROID_ROOT} AND EXISTS $ENV{ANDROID_ROOT})
139 option(LIBA_PIE "Enable/Disable position independent code" 1)
141 option(LIBA_PIE "Enable/Disable position independent code" 0)
144 # https://cmake.org/cmake/help/latest/module/CheckPIESupported.html
145 if(NOT CMAKE_VERSION VERSION_LESS 3.14)
146 cmake_policy(SET CMP0083 NEW)
147 include(CheckPIESupported)
148 check_pie_supported()
151 function(set_library_options)
152 if(LIBA_IWYU AND IWYU_FOUND)
153 add_include_what_you_use(TARGETS ${ARGN})
156 if(LIBA_CLANG_TIDY AND CLANG_TIDY_FOUND)
157 add_clang_tidy(TARGETS ${ARGN} OPTIONS --fix)
160 if(LIBA_CPPCHECK AND CPPCHECK_FOUND)
161 add_cppcheck(TARGETS ${ARGN} OPTIONS --enable=warning,performance)
164 set_target_properties(${ARGN} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${LIBA_IPO}
165 C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1
168 if(CMAKE_C_COMPILER_ID MATCHES "TinyCC" AND CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
169 set_property(TARGET ${ARGN} PROPERTY IMPORT_SUFFIX .def)
173 target_compile_warnings(${ARGN})
177 target_compile_analyzer(${ARGN})
181 function(add_library_properties target scope source)
182 target_compile_definitions(${target} ${scope} $<TARGET_PROPERTY:${source},${ARGV3}COMPILE_DEFINITIONS>)
183 target_include_directories(${target} ${scope} $<TARGET_PROPERTY:${source},${ARGV3}INCLUDE_DIRECTORIES>)
185 if(${scope} MATCHES "PUBLIC|PRIVATE")
186 set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES $<TARGET_PROPERTY:${source},${ARGV3}LINK_LIBRARIES>)
189 if(${scope} MATCHES "PUBLIC|INTERFACE")
190 set_property(TARGET ${target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<TARGET_PROPERTY:${source},${ARGV3}LINK_LIBRARIES>)
194 option(LIBA_MIMALLOC "Enable/Disable override using mimalloc" 0)
197 find_package(mimalloc)
201 if(TARGET mimalloc-static)
202 set(MIMALLOC_LIBRARY mimalloc-static)
204 set(MIMALLOC_LIBRARY mimalloc)
208 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git)
212 function(git_command output)
213 execute_process(ERROR_QUIET
214 COMMAND ${GIT_EXECUTABLE} ${ARGN}
215 OUTPUT_STRIP_TRAILING_WHITESPACE
216 OUTPUT_VARIABLE ${output}
218 set(${output} ${${output}} PARENT_SCOPE)
221 git_command(GIT_COMMIT_LONG log -1 --pretty=format:%H)
222 git_command(GIT_COMMIT_DATE log -1 --pretty=format:%cd --date=format:%Y%m%d)
226 set(${PROJECT_NAME}_VERSION_TWEAK ${GIT_COMMIT_DATE})
227 set(PROJECT_VERSION_TWEAK ${GIT_COMMIT_DATE})
228 set(A_VERSION_TWEAK ${GIT_COMMIT_DATE})
232 if(NOT EXISTS "${A_HAVE_H}")
233 set(A_HAVE_H ${CMAKE_CURRENT_BINARY_DIR}/a.cmake.h)
234 configure_file(${CMAKE_CURRENT_LIST_DIR}/include/a.cmake.h.in ${A_HAVE_H})
237 if(IS_ABSOLUTE "${LIBA_MATH}")
238 get_filename_component(math ${LIBA_MATH} NAME_WE)
239 string(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}(.+)" "\\1" math "${math}")
240 set(LIBA_MATH $<BUILD_INTERFACE:${LIBA_MATH}> $<INSTALL_INTERFACE:${math}>)
244 if(TARGET_SUPPORTS_SYMBOLIC_LINK)
245 option(LIBA_SYMLINK "Enable/Disable symbolic link" 1)
248 function(set_library_compile target)
249 if(EXISTS "${A_HAVE_H}")
250 file(RELATIVE_PATH a_have_h ${PROJECT_SOURCE_DIR}/include/a ${A_HAVE_H})
251 target_compile_definitions(${target} PUBLIC $<BUILD_INTERFACE:A_HAVE_H="${a_have_h}">)
255 if((LIBA_SYMLINK OR WIN32) AND TARGET_SUPPORTS_SYMBOLIC_LINK)
256 set_target_properties(${target} PROPERTIES SOVERSION
257 ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION}
261 set_target_properties(${target} PROPERTIES OUTPUT_NAME a
262 POSITION_INDEPENDENT_CODE 1 DEFINE_SYMBOL A_EXPORTS
264 target_compile_definitions(${target} PRIVATE A_EXPORTS)
265 target_include_directories(${target} BEFORE PUBLIC
266 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
267 $<INSTALL_INTERFACE:include>
269 get_property(TYPE TARGET ${target} PROPERTY TYPE)
271 if(${TYPE} MATCHES "(STATIC|SHARED|MODULE)_LIBRARY")
272 target_link_libraries(${target} PUBLIC ${LIBA_MATH})
275 set_library_options(${target})
279 file_scaner(SOURCES RECURSE include src EXT c h cc hh cpp hpp)
281 file_scaner(SOURCES RECURSE include src EXT c h)
284 add_library(a OBJECT ${SOURCES})
285 set_library_compile(a)
287 if(LIBA_MIMALLOC AND mimalloc_FOUND)
288 set_property(SOURCE ${CMAKE_CURRENT_LIST_DIR}/src/a.c
289 APPEND PROPERTY COMPILE_DEFINITIONS A_HAVE_MIMALLOC_H
292 if(CMAKE_VERSION VERSION_LESS 3.12)
293 add_library_properties(a PUBLIC ${MIMALLOC_LIBRARY} INTERFACE_)
295 target_link_libraries(a PUBLIC ${MIMALLOC_LIBRARY})
299 include(GNUInstallDirs)
300 set(INSTALL_TARGETS "")
302 if("${LIBA_INSTALL}" MATCHES "^$|static")
303 list(APPEND INSTALL_TARGETS alib)
306 add_library(alib STATIC $<TARGET_OBJECTS:a>)
307 set_library_compile(alib)
309 if(LIBA_MIMALLOC AND mimalloc_FOUND)
310 target_link_libraries(alib PUBLIC ${MIMALLOC_LIBRARY})
313 if(TARGET_SUPPORTS_SHARED_LIBS)
314 if("${LIBA_INSTALL}" MATCHES "^$|shared")
315 list(APPEND INSTALL_TARGETS liba)
318 add_library(liba SHARED $<TARGET_OBJECTS:a>)
319 set_library_compile(liba)
321 if(LIBA_MIMALLOC AND mimalloc_FOUND)
322 target_link_libraries(liba PUBLIC ${MIMALLOC_LIBRARY})
325 if(CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL .lib)
326 set_property(TARGET liba PROPERTY OUTPUT_NAME liba)
329 target_compile_definitions(liba INTERFACE A_IMPORTS)
332 target_link_static(liba)
337 add_library(asan OBJECT ${SOURCES})
338 set_library_compile(asan)
339 target_compile_sanitize(asan)
341 if(LIBA_MIMALLOC AND mimalloc_FOUND)
342 if(CMAKE_VERSION VERSION_LESS 3.12)
343 add_library_properties(asan PUBLIC ${MIMALLOC_LIBRARY} INTERFACE_)
345 target_link_libraries(asan PUBLIC ${MIMALLOC_LIBRARY})
351 include(cmake/InstallTargets.cmake)
352 install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/a DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
354 if(EXISTS "${A_HAVE_H}")
355 install(FILES ${A_HAVE_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/a)
356 install_hook(${CMAKE_INSTALL_INCLUDEDIR}/a/a.h A_HAVE_H ${A_HAVE_H})
360 option(LIBA_PKGCONFIG "Enable/Disable pkgconfig" 0)
363 include(cmake/InstallPkgConfig.cmake)
364 install_pkgconfig(${PROJECT_NAME} alib)
367 option(LIBA_LIBTOOL "Enable/Disable libtool" 0)
369 if(LIBA_LIBTOOL AND INSTALL_TARGETS)
370 include(cmake/InstallLibtool.cmake)
371 install_libtool(${INSTALL_TARGETS})
374 set(LIBA_VCPKG "" CACHE PATH "vcpkg installation root")
376 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/vcpkg AND LIBA_VCPKG AND GIT_COMMIT_LONG)
377 if(NOT EXISTS ${LIBA_VCPKG})
378 set(LIBA_VCPKG ${CMAKE_CURRENT_BINARY_DIR}/vcpkg)
379 install(DIRECTORY ${LIBA_VCPKG} DESTINATION ${CMAKE_INSTALL_DATADIR})
383 ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/portfile.cmake
384 ${LIBA_VCPKG}/ports/${PROJECT_NAME}/portfile.cmake
388 ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/vcpkg.json
389 ${LIBA_VCPKG}/ports/${PROJECT_NAME}/vcpkg.json
394 if(PROJECT_IS_TOP_LEVEL)
395 add_custom_target(afmt)
396 add_custom_target(adoc)
400 if(NOT CMAKE_FIND_LIBRARY_PREFIXES)
401 set(CMAKE_FIND_LIBRARY_PREFIXES ";lib")
404 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/java/CMakeLists.txt)
405 option(LIBA_JAVA "Enable/Disable java" 0)
412 if(LIBA_JAVA AND Java_JAVAC_EXECUTABLE)
413 add_subdirectory(java)
417 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/javascript/CMakeLists.txt)
419 option(LIBA_JAVASCRIPT "Enable/Disable javascript" 1)
421 option(LIBA_JAVASCRIPT "Enable/Disable javascript" 0)
426 find_package(Emscripten)
429 if(EMSCRIPTEN OR EMSCRIPTEN_FOUND)
430 add_subdirectory(javascript)
435 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/lua/CMakeLists.txt)
436 option(LIBA_LUA "Enable/Disable lua" 0)
437 option(LIBA_LUAJIT "Enable/Disable luajit" 0)
438 option(LIBA_WITH_LUA "Enable/Disable build with lua" 0)
440 if(LIBA_LUA OR LIBA_LUAJIT OR LIBA_WITH_LUA)
441 set(LUA_MATH_LIBRARY ${LIBA_MATH} CACHE STRING "lua math library")
444 if(NOT LIBA_WITH_LUA AND LIBA_LUA)
445 find_package(Lua OPTIONAL_COMPONENTS lua)
446 set(LUA_LIBRARIES lualib)
449 if((LIBA_LUA AND NOT LUA_FOUND AND NOT LIBA_WITH_LUA) OR LIBA_LUAJIT)
450 find_package(LuaJIT OPTIONAL_COMPONENTS luajit)
451 set(LUA_EXECUTABLE ${LUAJIT_EXECUTABLE})
452 set(LUA_LIBRARIES luajit)
455 if(LIBA_LUA AND LUA_FOUND)
456 add_subdirectory(lua)
457 elseif(LIBA_LUAJIT AND LUAJIT_FOUND)
458 add_subdirectory(lua)
459 elseif(LIBA_WITH_LUA)
460 add_subdirectory(lua)
464 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/python/CMakeLists.txt)
465 if(Python_FOUND AND CYTHON_FOUND)
466 add_subdirectory(python)
470 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/quickjs/CMakeLists.txt)
471 option(LIBA_QUICKJS "Enable/Disable quickjs" 0)
474 find_package(QuickJS)
477 if(LIBA_QUICKJS AND QUICKJS_FOUND)
478 add_subdirectory(quickjs)
482 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/test/CMakeLists.txt)
483 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
484 add_subdirectory(test)
488 set(CPACK_SOURCE_IGNORE_FILES ${CMAKE_CURRENT_LIST_DIR}/.git)
490 if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git AND GIT_FOUND)
491 execute_process(ERROR_QUIET
492 COMMAND ${GIT_EXECUTABLE} status -z --ignored=matching
493 OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE ignore
495 string(REPLACE "." "\\\\." ignore "${ignore}")
496 string(REPLACE "*" ".*" ignore "${ignore}")
497 string(REPLACE "?? " "!! " ignore "${ignore}")
498 string(REGEX MATCHALL "!! [^!]+" ignore "${ignore}")
499 string(REPLACE "!! " "${CMAKE_CURRENT_LIST_DIR}/" ignore "${ignore}")
500 list(APPEND CPACK_SOURCE_IGNORE_FILES ${ignore})
503 set(CPACK_RESOURCE_FILE_README ${CMAKE_CURRENT_LIST_DIR}/README.md)
504 set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_LIST_DIR}/LICENSE.txt)
507 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
508 find_package(ClangFormat)
511 if(CLANG_FORMAT_FOUND)
512 file_scaner(SOURCES EXT c h cc hh cpp hpp RECURSE include src test
513 java/src javascript/src lua/src quickjs/src
515 add_clang_format(afmt.c VERBOSE ${SOURCES})
516 add_dependencies(afmt afmt.c)
519 if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING AND LIBA_PYTHON)
524 add_black(afmt.py ${CMAKE_CURRENT_LIST_DIR})
525 add_dependencies(afmt afmt.py)
528 if(NOT CMAKE_VERSION VERSION_LESS 3.9 AND DOXYGEN_FOUND)
529 # Project related configuration options
530 set(DOXYGEN_CREATE_SUBDIRS YES)
531 set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
533 # Build related configuration options
534 set(DOXYGEN_EXTRACT_STATIC YES)
536 # Configuration options related to warning and progress messages
537 set(DOXYGEN_WARN_IF_UNDOCUMENTED NO)
538 set(DOXYGEN_WARN_NO_PARAMDOC YES)
540 # Configuration options related to the input files
541 set(DOXYGEN_RECURSIVE YES)
543 # Configuration options related to source browsing
544 # Configuration options related to the alphabetical class index
545 # Configuration options related to the HTML output
546 set(DOXYGEN_GENERATE_HTML YES)
547 set(DOXYGEN_USE_MATHJAX YES)
548 set(DOXYGEN_MATHJAX_VERSION MathJax_3)
550 # Configuration options related to the LaTeX output
551 # Configuration options related to the RTF output
552 # Configuration options related to the man page output
553 # Configuration options related to the XML output
554 set(DOXYGEN_GENERATE_XML YES)
556 # Configuration options related to the DOCBOOK output
557 # Configuration options for the AutoGen Definitions output
558 # Configuration options related to Sqlite3 output
559 # Configuration options related to the Perl module output
560 # Configuration options related to the preprocessor
561 set(DOXYGEN_MACRO_EXPANSION YES)
562 set(DOXYGEN_PREDEFINED
563 "__STDC_VERSION__=201112L"
564 "__cplusplus=201103L"
574 file(STRINGS "${A_HAVE_H}" macros REGEX "#define[ ]+A_HAVE_.+")
576 foreach(macro ${macros})
577 string(REGEX REPLACE "#define[ ]+(.*)[ ]+(.+)" "\\1=\\2" macro "${macro}")
578 list(APPEND DOXYGEN_PREDEFINED ${macro})
581 # Configuration options related to external references
582 # Configuration options related to the dot tool
583 if(Doxygen_dot_FOUND)
584 set(DOXYGEN_HAVE_DOT YES)
585 set(DOXYGEN_INTERACTIVE_SVG YES)
586 set(DOXYGEN_DOT_IMAGE_FORMAT svg)
587 set(DOXYGEN_DOT_MULTI_TARGETS YES)
590 # https://www.doxygen.nl/manual/config.html
591 doxygen_add_docs(adoc.c WORKING_DIRECTORY
592 ${CMAKE_CURRENT_LIST_DIR}/include/a
593 ${CMAKE_CURRENT_LIST_DIR}/include/a
594 ${CMAKE_CURRENT_LIST_DIR}/README.md
596 add_dependencies(adoc adoc.c)