Skip to content

Commit

Permalink
Merge pull request #493 from VShilenkov/master
Browse files Browse the repository at this point in the history
cmake: update CMake
  • Loading branch information
mvandervoord authored May 2, 2020
2 parents c5413ce + a2af08c commit 9f4b225
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 35 deletions.
128 changes: 115 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,126 @@
# License: MIT #
# #
###################################################################################
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

cmake_minimum_required(VERSION 3.12)

project(unity LANGUAGES C DESCRIPTION "C Unit testing framework.")
# Read src/unity.h file and get project version from it
set(UNITY_HEADER "src/unity.h")

add_subdirectory(src)
target_include_directories(unity
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
"$<INSTALL_INTERFACE:src>"
file(STRINGS "${UNITY_HEADER}" UNITY_HEADER_CONTENT
REGEX "^#define UNITY_VERSION_(MAJOR|MINOR|BUILD) +[0-9]+$"
)

set(UNITY_HEADER_VERSION_MAJOR 0)
set(UNITY_HEADER_VERSION_MINOR 0)
set(UNITY_HEADER_VERSION_BUILD 0)

foreach(VERSION_LINE IN LISTS UNITY_HEADER_CONTENT)
foreach(VERSION_PART MAJOR MINOR BUILD)
string(CONCAT REGEX_STRING "#define UNITY_VERSION_"
"${VERSION_PART}"
" +([0-9]+)"
)

PRIVATE "src"
if(VERSION_LINE MATCHES "${REGEX_STRING}")
set(UNITY_HEADER_VERSION_${VERSION_PART} "${CMAKE_MATCH_1}")
endif()
endforeach()
endforeach()

project(unity
VERSION ${UNITY_HEADER_VERSION_MAJOR}.${UNITY_HEADER_VERSION_MINOR}.${UNITY_HEADER_VERSION_BUILD}
LANGUAGES C
DESCRIPTION "C Unit testing framework."
)

add_library(unity::framework ALIAS unity)
# Main target ------------------------------------------------------------------
add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME}::framework ALIAS ${PROJECT_NAME})

# Includes ---------------------------------------------------------------------
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/ DESTINATION src)
install(EXPORT unityConfig DESTINATION share/unityConfig/cmake)
target_sources(${PROJECT_NAME}
PRIVATE
src/unity.c
)

# This makes the project importable from the build directory
export(TARGETS unity FILE unityConfig.cmake)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
)

set(${PROJECT_NAME}_PUBLIC_HEADERS src/unity.h
src/unity_internals.h
)

set_target_properties(${PROJECT_NAME}
PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
PUBLIC_HEADER "${${PROJECT_NAME}_PUBLIC_HEADERS}"
EXPORT_NAME framework
)

target_compile_options(${PROJECT_NAME}
PRIVATE
$<$<C_COMPILER_ID:Clang>:-Wcast-align
-Wcast-qual
-Wconversion
-Wexit-time-destructors
-Wglobal-constructors
-Wmissing-noreturn
-Wmissing-prototypes
-Wno-missing-braces
-Wold-style-cast
-Wshadow
-Wweak-vtables>
$<$<C_COMPILER_ID:GNU>:-Waddress
-Waggregate-return
-Wformat-nonliteral
-Wformat-security
-Wformat
-Winit-self
-Wmissing-declarations
-Wmissing-include-dirs
-Wno-multichar
-Wno-parentheses
-Wno-type-limits
-Wno-unused-parameter
-Wunreachable-code
-Wwrite-strings
-Wpointer-arith>
-Wall
-Werror
)

write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)

## Target installation
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
COMPONENT library
)

## Target's cmake files: targets export
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

## Target's cmake files: config and version config for find_package()
install(FILES ${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
22 changes: 0 additions & 22 deletions src/CMakeLists.txt

This file was deleted.

1 change: 1 addition & 0 deletions unityConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/unityTargets.cmake)

0 comments on commit 9f4b225

Please sign in to comment.