From 49870534bae0199c1282ec2424670450eb854afe Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 3 Feb 2022 09:29:14 +0100 Subject: [PATCH] Update CMake list file Minimum required CMake version is now 3.16. --- CMakeLists.txt | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 10d5ede..910286f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,51 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.16) project(tinf C) +include(CheckCCompilerFlag) include(CTest) # Check if tinf is the top-level project (standalone), or a subproject -set(tinf_standalone FALSE) -get_directory_property(tinf_parent_directory PARENT_DIRECTORY) -if(tinf_parent_directory STREQUAL "") - set(tinf_standalone TRUE) +set(_tinf_standalone FALSE) +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + set(_tinf_standalone TRUE) endif() -unset(tinf_parent_directory) # TINF_BUILD_TESTING controls if tinf adds testing support # # When built standalone, it defaults to the value of BUILD_TESTING if set. # An optional prefix for the test names can be set with TINF_TEST_PREFIX. -set(tinf_testing_default ON) -if(tinf_standalone) +set(_tinf_testing_default ON) +if(_tinf_standalone) if(DEFINED BUILD_TESTING) - set(tinf_testing_default ${BUILD_TESTING}) + set(_tinf_testing_default ${BUILD_TESTING}) endif() else() - set(tinf_testing_default OFF) + set(_tinf_testing_default OFF) endif() -option(TINF_BUILD_TESTING "Add testing support" ${tinf_testing_default}) -unset(tinf_testing_default) +option(TINF_BUILD_TESTING "Add testing support" ${_tinf_testing_default}) +unset(_tinf_testing_default) mark_as_advanced(TINF_TEST_PREFIX) +# Take a list of compiler flags and add those which the compiler accepts to +# the COMPILE_OPTIONS directory property +function(add_valid_c_compile_options) + foreach(flag IN LISTS ARGN) + string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" flag_var "CFLAG_${flag}") + check_c_compiler_flag("${flag}" ${flag_var}) + if(${flag_var}) + add_compile_options("${flag}") + endif() + endforeach() +endfunction() + +if(MSVC) + add_valid_c_compile_options(/W3) +else() + add_valid_c_compile_options(-Wall -Wextra -pedantic) +endif() + # # tinf # @@ -40,13 +57,13 @@ add_library(tinf src/tinfzlib.c src/tinf.h ) -target_include_directories(tinf PUBLIC $) +target_include_directories(tinf PUBLIC $) # # tgunzip # add_executable(tgunzip examples/tgunzip/tgunzip.c) -target_link_libraries(tgunzip tinf) +target_link_libraries(tgunzip PRIVATE tinf) if(MSVC) target_compile_definitions(tgunzip PRIVATE _CRT_SECURE_NO_WARNINGS) endif() @@ -56,7 +73,7 @@ endif() # if(TINF_BUILD_TESTING) add_executable(test_tinf test/test_tinf.c) - target_link_libraries(test_tinf tinf) + target_link_libraries(test_tinf PRIVATE tinf) if(MSVC) target_compile_definitions(test_tinf PRIVATE _CRT_SECURE_NO_WARNINGS) endif()