2017-11-26 23:09:46 -05:00
cmake_minimum_required ( VERSION 3.0.2 )
2017-11-24 21:34:36 -05:00
project ( date_prj )
2017-11-24 23:07:39 -05:00
find_package ( Threads REQUIRED )
2017-11-24 21:34:36 -05:00
2017-11-26 23:09:46 -05:00
enable_testing ( )
2017-11-24 21:34:36 -05:00
2017-11-24 23:07:39 -05:00
option ( USE_SYSTEM_TZ_DB "Use the operating system's timezone database" OFF )
option ( USE_TZ_DB_IN_DOT "Save the timezone database in the current folder" OFF )
2017-12-02 20:24:42 +01:00
option ( BUILD_TZ_STATIC "Build a static version of library" ON )
function ( print_option OPT )
2017-12-26 21:00:29 +01:00
if ( NOT DEFINED PRINT_OPTION_CURR_ ${ OPT } OR ( NOT PRINT_OPTION_CURR_ ${ OPT } STREQUAL ${ OPT } ) )
set ( PRINT_OPTION_CURR_ ${ OPT } ${ ${OPT } } CACHE BOOL "" )
mark_as_advanced ( PRINT_OPTION_CURR_ ${ OPT } )
2017-12-02 20:24:42 +01:00
message ( "# date: ${OPT} ${${OPT}}" )
endif ( )
endfunction ( )
print_option ( USE_SYSTEM_TZ_DB )
print_option ( USE_TZ_DB_IN_DOT )
print_option ( BUILD_TZ_STATIC )
2017-11-24 21:34:36 -05:00
2017-11-29 15:27:24 +01:00
if ( USE_SYSTEM_TZ_DB )
2017-11-24 21:34:36 -05:00
add_definitions ( -DUSE_AUTOLOAD=0 )
add_definitions ( -DHAS_REMOTE_API=0 )
2017-11-24 23:58:18 -05:00
# cannot set USE_OS_TZDB to 1 on Windows
2017-11-29 15:27:24 +01:00
if ( NOT WIN32 )
2017-11-24 23:54:38 -05:00
add_definitions ( -DUSE_OS_TZDB=1 )
endif ( )
2017-11-24 21:34:36 -05:00
else ( )
2017-11-24 23:07:39 -05:00
add_definitions ( -DUSE_AUTOLOAD=1 )
add_definitions ( -DHAS_REMOTE_API=1 )
add_definitions ( -DUSE_OS_TZDB=0 )
2017-11-24 21:34:36 -05:00
find_package ( CURL REQUIRED )
include_directories ( SYSTEM ${ CURL_INCLUDE_DIRS } )
2017-11-26 15:44:22 -05:00
set ( OPTIONAL_LIBRARIES ${ CURL_LIBRARIES } )
2017-11-24 21:34:36 -05:00
endif ( )
2017-11-29 15:27:24 +01:00
if ( USE_TZ_DB_IN_DOT )
2017-11-24 23:07:39 -05:00
add_definitions ( -DINSTALL=. )
endif ( )
2017-11-24 21:34:36 -05:00
set ( HEADER_FOLDER "include" )
set ( SOURCE_FOLDER "src" )
set ( TEST_FOLDER "test" )
# This is needed so IDE's live MSVC show header files
set ( HEADER_FILES
$ { H E A D E R _ F O L D E R } / d a t e / c h r o n o _ i o . h
$ { H E A D E R _ F O L D E R } / d a t e / d a t e . h
$ { H E A D E R _ F O L D E R } / d a t e / i o s . h
$ { H E A D E R _ F O L D E R } / d a t e / i s l a m i c . h
$ { H E A D E R _ F O L D E R } / d a t e / i s o _ w e e k . h
$ { H E A D E R _ F O L D E R } / d a t e / j u l i a n . h
$ { H E A D E R _ F O L D E R } / d a t e / t z . h
$ { H E A D E R _ F O L D E R } / d a t e / t z _ p r i v a t e . h
)
2017-12-02 20:24:42 +01:00
if ( BUILD_TZ_STATIC )
2017-11-24 23:24:19 -05:00
add_library ( tz STATIC ${ HEADER_FILES } ${ SOURCE_FOLDER } /tz.cpp )
else ( )
add_library ( tz SHARED ${ HEADER_FILES } ${ SOURCE_FOLDER } /tz.cpp )
endif ( )
2017-12-25 17:14:50 +02:00
if ( ${ CMAKE_MINOR_VERSION } GREATER 7 )
set ( TZ_CXX_STANDARD 17 )
else ( )
set ( TZ_CXX_STANDARD 14 )
endif ( )
set_property ( TARGET tz PROPERTY CXX_STANDARD ${ TZ_CXX_STANDARD } )
2017-12-02 20:24:42 +01:00
target_link_libraries ( tz ${ CMAKE_THREAD_LIBS_INIT } ${ OPTIONAL_LIBRARIES } )
2017-11-24 21:34:36 -05:00
2017-11-29 15:27:24 +01:00
# add include folders to the library and targets that consume it
target_include_directories ( tz PUBLIC
$ < B U I L D _ I N T E R F A C E :
$ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / $ { H E A D E R _ F O L D E R }
>
$ < I N S T A L L _ I N T E R F A C E :
i n c l u d e
>
)
2017-12-02 20:24:42 +01:00
add_library ( date_interface INTERFACE ) # an interface (not a library), to enable automatic include_directory (for when just date.h, but not "tz.h and its lib" are needed)
2017-12-01 21:15:32 +01:00
# add include folders to the INTERFACE and targets that consume it
2017-12-02 20:24:42 +01:00
target_include_directories ( date_interface INTERFACE
2017-12-01 21:15:32 +01:00
$ < B U I L D _ I N T E R F A C E :
$ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } / $ { H E A D E R _ F O L D E R }
>
$ < I N S T A L L _ I N T E R F A C E :
i n c l u d e
>
)
2017-11-24 21:34:36 -05:00
install ( TARGETS tz DESTINATION lib )
install ( DIRECTORY ${ HEADER_FOLDER } / DESTINATION include/ )
2017-11-24 23:07:39 -05:00
2017-11-26 23:09:46 -05:00
add_custom_target ( testit COMMAND ${ CMAKE_CTEST_COMMAND } )
add_dependencies ( testit tz )
function ( add_pass_tests TEST_GLOB TEST_PREFIX )
2017-11-27 00:00:14 -05:00
file ( GLOB_RECURSE FILENAMES ${ TEST_GLOB } )
2017-11-26 23:09:46 -05:00
include_directories ( "${HEADER_FOLDER}/date" )
foreach ( TEST_FILE ${ FILENAMES } )
get_filename_component ( TEST_NAME ${ TEST_FILE } NAME_WE )
get_filename_component ( TEST_EXT ${ TEST_FILE } EXT )
2017-11-29 15:27:24 +01:00
if ( NOT ${ TEST_EXT } STREQUAL ".fail.cpp" )
2017-11-27 00:16:24 -05:00
set ( PREFIX "${TEST_PREFIX}_pass_${TEST_NAME}" )
2017-11-27 00:11:03 -05:00
set ( BIN_NAME ${ PREFIX } _bin )
set ( TST_NAME ${ PREFIX } _test )
add_executable ( ${ BIN_NAME } EXCLUDE_FROM_ALL ${ TEST_FILE } )
2017-12-25 17:14:50 +02:00
set_property ( TARGET ${ BIN_NAME } PROPERTY CXX_STANDARD ${ TZ_CXX_STANDARD } )
2017-11-27 00:11:03 -05:00
add_test ( ${ TST_NAME } ${ BIN_NAME } )
target_link_libraries ( ${ BIN_NAME } tz )
add_dependencies ( testit ${ BIN_NAME } )
endif ( )
2017-11-26 23:09:46 -05:00
endforeach ( )
endfunction ( )
function ( add_fail_tests TEST_GLOB TEST_PREFIX )
2017-11-27 00:00:14 -05:00
file ( GLOB_RECURSE FILENAMES ${ TEST_GLOB } )
2017-11-26 23:09:46 -05:00
foreach ( TEST_FILE ${ FILENAMES } )
get_filename_component ( TEST_NAME ${ TEST_FILE } NAME_WE )
get_filename_component ( TEST_EXT ${ TEST_FILE } EXT )
set ( TEST_TYPE "_fail" )
2017-11-27 00:16:24 -05:00
set ( PREFIX "${TEST_PREFIX}_fail_${TEST_NAME}" )
2017-11-26 23:09:46 -05:00
set ( BIN_NAME ${ PREFIX } _bin )
set ( TST_NAME ${ PREFIX } _test )
#target_compile_definitions( ${BIN_NAME} PRIVATE ${TST_NAME} )
set ( TEST_BIN_NAME ${ CMAKE_BINARY_DIR } / ${ BIN_NAME } )
2017-11-29 15:27:24 +01:00
add_custom_target ( ${ BIN_NAME }
2017-11-26 23:09:46 -05:00
C O M M A N D $ { P R O J E C T _ S O U R C E _ D I R } / c o m p i l e _ f a i l . s h $ { T E S T _ B I N _ N A M E } $ { C M A K E _ C X X _ C O M P I L E R } - s t d = c + + 1 4 - L $ { C M A K E _ B I N A R Y _ D I R } / - l t z - I $ { P R O J E C T _ S O U R C E _ D I R } / $ { H E A D E R _ F O L D E R } / d a t e - o $ { B I N _ N A M E } $ { T E S T _ F I L E }
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ B I N A R Y _ D I R }
2017-11-29 15:27:24 +01:00
C O M M E N T $ { T S T _ N A M E }
2017-11-26 23:09:46 -05:00
)
add_test ( ${ TST_NAME } "${PROJECT_SOURCE_DIR}/test_fail.sh" ${ CMAKE_BINARY_DIR } / ${ BIN_NAME } WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/" )
#set_tests_properties( ${TST_NAME} PROPERTIES WILL_FAIL TRUE)
add_dependencies ( testit ${ BIN_NAME } )
endforeach ( )
endfunction ( )
2017-11-27 00:00:14 -05:00
file ( GLOB children RELATIVE "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}" "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/*" )
foreach ( child ${ children } )
if ( IS_DIRECTORY "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/${child}" )
set ( CUR_FOLDER "${PROJECT_SOURCE_DIR}/${TEST_FOLDER}/${child}" )
2017-11-27 00:11:03 -05:00
add_pass_tests ( "${CUR_FOLDER}/*.cpp" ${ child } )
2017-11-29 15:27:24 +01:00
if ( NOT WIN32 )
2017-11-27 00:00:14 -05:00
add_fail_tests ( "${CUR_FOLDER}/*.fail.cpp" ${ child } )
endif ( )
endif ( )
endforeach ( )
2017-11-26 23:09:46 -05:00