[enhancement] updated cmake checks (#581)

* added header file checks

* cmake cleanup and better use of checks

* fixed bug in numerical_methods/CMake

* add docs to cmake
This commit is contained in:
Krishna Vedala 2020-07-23 10:44:18 -04:00 committed by GitHub
parent 296f3d00d0
commit 3c1b585656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 49 deletions

View File

@ -5,6 +5,7 @@ project(Algorithms_in_C
DESCRIPTION "Set of algorithms implemented in C." DESCRIPTION "Set of algorithms implemented in C."
) )
# Set compilation standards
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES) set(CMAKE_C_STANDARD_REQUIRED YES)
@ -13,8 +14,12 @@ if(MSVC)
# add_compile_options(/Za) # add_compile_options(/Za)
endif(MSVC) endif(MSVC)
# check for math library
# addresses a bug when linking on OSX
find_library(MATH_LIBRARY m) find_library(MATH_LIBRARY m)
# Optional flag - can be set by user
# Default "ON"
option(USE_OPENMP "flag to use OpenMP for multithreading" ON) option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
if(USE_OPENMP) if(USE_OPENMP)
find_package(OpenMP) find_package(OpenMP)
@ -25,6 +30,24 @@ if(USE_OPENMP)
endif() endif()
endif() endif()
## Check for some required header files
include(CheckIncludeFile)
include(CheckSymbolExists)
check_include_file(stdbool.h HAS_STDBOOL_H)
check_include_file(inttypes.h HAS_INTTYPES_H)
check_include_file(complex.h HAS_COMPLEX_H)
if(HAS_COMPLEX_H)
check_symbol_exists(complex complex.h HAS_COMPLEX_TYPE)
endif(HAS_COMPLEX_H)
if (NOT HAS_STDBOOL_H)
message(FATAL_ERROR "Missing required header: 'stdbool.h'")
endif()
if (NOT HAS_INTTYPES_H)
message(FATAL_ERROR "Missing required header: 'inttypes.h'")
endif()
## Add subdirectories containing CMakeLists.txt
# to configure and compile files in the respective folders
add_subdirectory(misc) add_subdirectory(misc)
add_subdirectory(sorting) add_subdirectory(sorting)
add_subdirectory(graphics) add_subdirectory(graphics)
@ -35,6 +58,7 @@ add_subdirectory(project_euler)
add_subdirectory(machine_learning) add_subdirectory(machine_learning)
add_subdirectory(numerical_methods) add_subdirectory(numerical_methods)
## Configure Doxygen documentation system
cmake_policy(SET CMP0054 NEW) cmake_policy(SET CMP0054 NEW)
cmake_policy(SET CMP0057 NEW) cmake_policy(SET CMP0057 NEW)
find_package(Doxygen OPTIONAL_COMPONENTS dot dia) find_package(Doxygen OPTIONAL_COMPONENTS dot dia)
@ -77,7 +101,7 @@ if(DOXYGEN_FOUND)
) )
endif() endif()
## Enable tool to generate binary distribution files
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack) include(CPack)

View File

@ -1,14 +1,10 @@
# include(CheckIncludeFile) if(WIN32)
# check_include_file(arpa/inet.h ARPA_HEADERS) check_include_file(winsock2.h WINSOCK_HEADER)
# if(NOT ARPA_HEADERS) else()
# check_include_file(winsock2.h WINSOCK_HEADER) check_include_file(arpa/inet.h ARPA_HEADERS)
# if(NOT WINSOCK_HEADER) endif()
# message(FATAL_ERROR "socket headers not found in system.")
# endif()
# endif()
# check_include_file(unistd.h HAS_UNISTD)
if(ARPA_HEADERS OR WINSOCK_HEADER)
# If necessary, use the RELATIVE flag, otherwise each source file may be listed # If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name # with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically. # automatically.
@ -27,19 +23,22 @@ foreach( testsourcefile ${APP_SOURCES} )
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY}) target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
endif() endif()
if(HAS_UNISTD) # if(HAS_UNISTD)
target_compile_definitions(${testname} PRIVATE HAS_UNISTD) # target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
endif() # endif()
# if(ARPA_HEADERS) # if(ARPA_HEADERS)
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS) # target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
# else() # else()
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER) # target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
# endif() # endif()
if(WIN32) if(WINSOCK_HEADER)
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
endif() endif()
install(TARGETS ${testname} DESTINATION "bin/client_server") install(TARGETS ${testname} DESTINATION "bin/client_server")
endforeach( testsourcefile ${APP_SOURCES} ) endforeach( testsourcefile ${APP_SOURCES} )
else()
message(WARNING "socket headers not found in system.")
endif(ARPA_HEADERS OR WINSOCK_HEADER)

View File

@ -5,11 +5,15 @@ file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
set (no_msvc "newton_raphson_root.c" "durand_kerner_roots.c") # List of files that use complex.h and complex data type
set (NEED_COMPLEX
"newton_raphson_root.c"
"durand_kerner_roots.c"
)
foreach( testsourcefile ${APP_SOURCES} ) foreach( testsourcefile ${APP_SOURCES} )
# Do not compile these files that use complex.h on MSVC # compile files that use complex.h only if available
if ( ${testsourcefile} IN_LIST no_msvc AND MSVC) if ( ${testsourcefile} IN_LIST NEED_COMPLEX AND NOT HAS_COMPLEX_TYPE)
continue() continue()
endif() endif()
string( REPLACE ".c" "" testname ${testsourcefile} ) string( REPLACE ".c" "" testname ${testsourcefile} )

View File

@ -1,7 +1,3 @@
if(USE_OPENMP)
find_package(OpenMP)
endif()
# If necessary, use the RELATIVE flag, otherwise each source file may be listed # If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name # with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically. # automatically.