Skip to content

Commit 3c1b585

Browse files
authored
[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
1 parent 296f3d0 commit 3c1b585

File tree

4 files changed

+72
-49
lines changed

4 files changed

+72
-49
lines changed

CMakeLists.txt

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(Algorithms_in_C
55
DESCRIPTION "Set of algorithms implemented in C."
66
)
77

8+
# Set compilation standards
89
set(CMAKE_C_STANDARD 11)
910
set(CMAKE_C_STANDARD_REQUIRED YES)
1011

@@ -13,8 +14,12 @@ if(MSVC)
1314
# add_compile_options(/Za)
1415
endif(MSVC)
1516

17+
# check for math library
18+
# addresses a bug when linking on OSX
1619
find_library(MATH_LIBRARY m)
1720

21+
# Optional flag - can be set by user
22+
# Default "ON"
1823
option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
1924
if(USE_OPENMP)
2025
find_package(OpenMP)
@@ -25,6 +30,24 @@ if(USE_OPENMP)
2530
endif()
2631
endif()
2732

33+
## Check for some required header files
34+
include(CheckIncludeFile)
35+
include(CheckSymbolExists)
36+
check_include_file(stdbool.h HAS_STDBOOL_H)
37+
check_include_file(inttypes.h HAS_INTTYPES_H)
38+
check_include_file(complex.h HAS_COMPLEX_H)
39+
if(HAS_COMPLEX_H)
40+
check_symbol_exists(complex complex.h HAS_COMPLEX_TYPE)
41+
endif(HAS_COMPLEX_H)
42+
if (NOT HAS_STDBOOL_H)
43+
message(FATAL_ERROR "Missing required header: 'stdbool.h'")
44+
endif()
45+
if (NOT HAS_INTTYPES_H)
46+
message(FATAL_ERROR "Missing required header: 'inttypes.h'")
47+
endif()
48+
49+
## Add subdirectories containing CMakeLists.txt
50+
# to configure and compile files in the respective folders
2851
add_subdirectory(misc)
2952
add_subdirectory(sorting)
3053
add_subdirectory(graphics)
@@ -35,6 +58,7 @@ add_subdirectory(project_euler)
3558
add_subdirectory(machine_learning)
3659
add_subdirectory(numerical_methods)
3760

61+
## Configure Doxygen documentation system
3862
cmake_policy(SET CMP0054 NEW)
3963
cmake_policy(SET CMP0057 NEW)
4064
find_package(Doxygen OPTIONAL_COMPONENTS dot dia)
@@ -77,7 +101,7 @@ if(DOXYGEN_FOUND)
77101
)
78102
endif()
79103

80-
104+
## Enable tool to generate binary distribution files
81105
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
82106
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
83107
include(CPack)

client_server/CMakeLists.txt

+40-41
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
# include(CheckIncludeFile)
2-
# check_include_file(arpa/inet.h ARPA_HEADERS)
3-
# if(NOT ARPA_HEADERS)
4-
# check_include_file(winsock2.h WINSOCK_HEADER)
5-
# if(NOT WINSOCK_HEADER)
6-
# message(FATAL_ERROR "socket headers not found in system.")
7-
# endif()
8-
# endif()
1+
if(WIN32)
2+
check_include_file(winsock2.h WINSOCK_HEADER)
3+
else()
4+
check_include_file(arpa/inet.h ARPA_HEADERS)
5+
endif()
96

10-
# check_include_file(unistd.h HAS_UNISTD)
7+
if(ARPA_HEADERS OR WINSOCK_HEADER)
8+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
9+
# with full pathname. RELATIVE may makes it easier to extract an executable name
10+
# automatically.
11+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
12+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
13+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
14+
foreach( testsourcefile ${APP_SOURCES} )
15+
# I used a simple string replace, to cut off .cpp.
16+
string( REPLACE ".c" "" testname ${testsourcefile} )
17+
add_executable( ${testname} ${testsourcefile} )
18+
19+
if(OpenMP_C_FOUND)
20+
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
21+
endif()
22+
if(MATH_LIBRARY)
23+
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
24+
endif()
25+
26+
# if(HAS_UNISTD)
27+
# target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
28+
# endif()
29+
# if(ARPA_HEADERS)
30+
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
31+
# else()
32+
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
33+
# endif()
1134

12-
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
13-
# with full pathname. RELATIVE may makes it easier to extract an executable name
14-
# automatically.
15-
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
16-
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
17-
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
18-
foreach( testsourcefile ${APP_SOURCES} )
19-
# I used a simple string replace, to cut off .cpp.
20-
string( REPLACE ".c" "" testname ${testsourcefile} )
21-
add_executable( ${testname} ${testsourcefile} )
22-
23-
if(OpenMP_C_FOUND)
24-
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
25-
endif()
26-
if(MATH_LIBRARY)
27-
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
28-
endif()
29-
30-
if(HAS_UNISTD)
31-
target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
32-
endif()
33-
# if(ARPA_HEADERS)
34-
# target_compile_definitions(${testname} PRIVATE ARPA_HEADERS)
35-
# else()
36-
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
37-
# endif()
35+
if(WINSOCK_HEADER)
36+
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
37+
endif()
3838

39-
if(WIN32)
40-
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
41-
endif()
39+
install(TARGETS ${testname} DESTINATION "bin/client_server")
4240

43-
install(TARGETS ${testname} DESTINATION "bin/client_server")
44-
45-
endforeach( testsourcefile ${APP_SOURCES} )
41+
endforeach( testsourcefile ${APP_SOURCES} )
42+
else()
43+
message(WARNING "socket headers not found in system.")
44+
endif(ARPA_HEADERS OR WINSOCK_HEADER)

numerical_methods/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
55
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
66
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
77

8-
set (no_msvc "newton_raphson_root.c" "durand_kerner_roots.c")
8+
# List of files that use complex.h and complex data type
9+
set (NEED_COMPLEX
10+
"newton_raphson_root.c"
11+
"durand_kerner_roots.c"
12+
)
913

1014
foreach( testsourcefile ${APP_SOURCES} )
11-
# Do not compile these files that use complex.h on MSVC
12-
if ( ${testsourcefile} IN_LIST no_msvc AND MSVC)
15+
# compile files that use complex.h only if available
16+
if ( ${testsourcefile} IN_LIST NEED_COMPLEX AND NOT HAS_COMPLEX_TYPE)
1317
continue()
1418
endif()
1519
string( REPLACE ".c" "" testname ${testsourcefile} )

sorting/CMakeLists.txt

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

0 commit comments

Comments
 (0)