mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
[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:
parent
296f3d00d0
commit
3c1b585656
@ -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)
|
||||||
|
@ -1,45 +1,44 @@
|
|||||||
# 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
|
||||||
|
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
||||||
|
# automatically.
|
||||||
|
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
||||||
|
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
||||||
|
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
||||||
|
foreach( testsourcefile ${APP_SOURCES} )
|
||||||
|
# I used a simple string replace, to cut off .cpp.
|
||||||
|
string( REPLACE ".c" "" testname ${testsourcefile} )
|
||||||
|
add_executable( ${testname} ${testsourcefile} )
|
||||||
|
|
||||||
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
|
if(OpenMP_C_FOUND)
|
||||||
# with full pathname. RELATIVE may makes it easier to extract an executable name
|
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
|
||||||
# automatically.
|
endif()
|
||||||
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
|
if(MATH_LIBRARY)
|
||||||
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
|
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
|
||||||
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
|
endif()
|
||||||
foreach( testsourcefile ${APP_SOURCES} )
|
|
||||||
# I used a simple string replace, to cut off .cpp.
|
|
||||||
string( REPLACE ".c" "" testname ${testsourcefile} )
|
|
||||||
add_executable( ${testname} ${testsourcefile} )
|
|
||||||
|
|
||||||
if(OpenMP_C_FOUND)
|
# if(HAS_UNISTD)
|
||||||
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
|
# target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
|
||||||
endif()
|
# endif()
|
||||||
if(MATH_LIBRARY)
|
# if(ARPA_HEADERS)
|
||||||
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
|
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
|
||||||
endif()
|
# else()
|
||||||
|
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
|
||||||
|
# endif()
|
||||||
|
|
||||||
if(HAS_UNISTD)
|
if(WINSOCK_HEADER)
|
||||||
target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
|
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
|
||||||
endif()
|
endif()
|
||||||
# if(ARPA_HEADERS)
|
|
||||||
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
|
|
||||||
# else()
|
|
||||||
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
|
|
||||||
# endif()
|
|
||||||
|
|
||||||
if(WIN32)
|
install(TARGETS ${testname} DESTINATION "bin/client_server")
|
||||||
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
|
|
||||||
endif()
|
|
||||||
|
|
||||||
install(TARGETS ${testname} DESTINATION "bin/client_server")
|
endforeach( testsourcefile ${APP_SOURCES} )
|
||||||
|
else()
|
||||||
endforeach( testsourcefile ${APP_SOURCES} )
|
message(WARNING "socket headers not found in system.")
|
||||||
|
endif(ARPA_HEADERS OR WINSOCK_HEADER)
|
||||||
|
@ -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} )
|
||||||
|
@ -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.
|
||||||
|
Loading…
Reference in New Issue
Block a user