|
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() |
9 | 6 |
|
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() |
11 | 34 |
|
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() |
38 | 38 |
|
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") |
42 | 40 |
|
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) |
0 commit comments