From f7643ada270fb9a788f0abf2c55e5ed6d9c35abb Mon Sep 17 00:00:00 2001 From: doodspav Date: Sat, 11 Mar 2023 04:10:36 +0000 Subject: [PATCH] GHI #11 Setup CMake for building project --- CMakeLists.txt | 73 ++++++++++++++++++++++++++ cmake/util/OptionVariables.cmake | 30 +++++++++++ cmake/util/PreventInSourceBuilds.cmake | 9 ++++ cmake/util/ProjectIsTopLevel.cmake | 6 +++ include/patomic/patomic.h | 22 ++++++++ src/patomic.c | 11 ++++ 6 files changed, 151 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/util/OptionVariables.cmake create mode 100644 cmake/util/PreventInSourceBuilds.cmake create mode 100644 cmake/util/ProjectIsTopLevel.cmake create mode 100644 include/patomic/patomic.h create mode 100644 src/patomic.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..9acfaa22d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,73 @@ +cmake_minimum_required(VERSION 3.14) + +include(cmake/util/PreventInSourceBuilds.cmake) + +project( + patomic + VERSION 0.5.1 + DESCRIPTION "Portable C90 Atomics Library" + HOMEPAGE_URL "https://github.com/doodspav/patomic" + LANGUAGES C +) + +# don't change include order, OptionVariables checks if project is top level +include(cmake/util/ProjectIsTopLevel.cmake) +include(cmake/util/OptionVariables.cmake) + + +# ---- Declare Library ---- + +add_library( + patomic_patomic + # include + include/patomic/patomic.h + # src + src/patomic.c +) + +# alias to cause error at configuration time instead of link time if target is missing +add_library(patomic::patomic ALIAS patomic_patomic) + + +# ---- Generate Build Info Headers ---- + +# used in export header generated below +if(NOT BUILD_SHARED_LIBS) + target_compile_definitions(patomic_patomic PUBLIC PATOMIC_STATIC_DEFINE) +endif() + +include(GenerateExportHeader) +generate_export_header( + patomic_patomic + BASE_NAME patomic + EXPORT_FILE_NAME include/patomic/patomic_export.h +) + + +# ---- Library Properties ---- + +set_target_properties( + patomic_patomic PROPERTIES + C_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN YES + VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}" + EXPORT_NAME patomic + OUTPUT_NAME patomic +) + +# header files generated by CMake +target_include_directories( + patomic_patomic SYSTEM + PUBLIC + "$" +) + +# header files from /include +target_include_directories( + patomic_patomic ${warning_guard} + PUBLIC + "$" +) + +target_compile_features(patomic_patomic PUBLIC c_std_90) diff --git a/cmake/util/OptionVariables.cmake b/cmake/util/OptionVariables.cmake new file mode 100644 index 000000000..0a81c559d --- /dev/null +++ b/cmake/util/OptionVariables.cmake @@ -0,0 +1,30 @@ +# ---- Developer Mode ---- + +# Developer mode enables targets and code paths in the CMake scripts that are +# only relevant for the developer(s) of patomic. +# Targets necessary to build the project must be provided unconditionally, so +# that consumers can trivially build and package the project. +if(PROJECT_IS_TOP_LEVEL) + option(patomic_DEVELOPER_MODE "Enable developer mode" OFF) + option(BUILD_SHARED_LIBS "Build shared libs" OFF) +endif() + + +# ---- Warning Guard ---- + +# target_include_directories with SYSTEM modifier will request the compiler to +# omit warnings from the provided paths, if the compiler supports that. +# This is to provide a user experience similar to find_package when +# add_subdirectory or FetchContent is used to consume this project. +set(warning_guard "") +if(NOT PROJECT_IS_TOP_LEVEL) + option( + patomic_INCLUDES_WITH_SYSTEM + "Use SYSTEM modifier for patomic's includes, disabling warnings" + ON + ) + mark_as_advanced(patomic_INCLUDES_WITH_SYSTEM) + if(patomic_INCLUDES_WITH_SYSTEM) + set(warning_guard SYSTEM) + endif() +endif() diff --git a/cmake/util/PreventInSourceBuilds.cmake b/cmake/util/PreventInSourceBuilds.cmake new file mode 100644 index 000000000..c78f18825 --- /dev/null +++ b/cmake/util/PreventInSourceBuilds.cmake @@ -0,0 +1,9 @@ +# in-source build guard +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) + message( + FATAL_ERROR + "In-source builds are not supported. " + "Please read the BUILDING document before trying to build this project. " + "You may need to delete 'CMakeCache.txt' and 'CMakeFiles/' first." + ) +endif() diff --git a/cmake/util/ProjectIsTopLevel.cmake b/cmake/util/ProjectIsTopLevel.cmake new file mode 100644 index 000000000..fc2ef6c9f --- /dev/null +++ b/cmake/util/ProjectIsTopLevel.cmake @@ -0,0 +1,6 @@ +# this variable is set by project() in CMake 3.21+ +string( + COMPARE EQUAL + "${CMAKE_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}" + PROJECT_IS_TOP_LEVEL +) diff --git a/include/patomic/patomic.h b/include/patomic/patomic.h new file mode 100644 index 000000000..2ce79ea60 --- /dev/null +++ b/include/patomic/patomic.h @@ -0,0 +1,22 @@ +#ifndef PATOMIC_PATOMIC_H +#define PATOMIC_PATOMIC_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +PATOMIC_EXPORT int +patomic_example_add( + int a, + int b +); + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PATOMIC_PATOMIC_H */ diff --git a/src/patomic.c b/src/patomic.c new file mode 100644 index 000000000..404519e55 --- /dev/null +++ b/src/patomic.c @@ -0,0 +1,11 @@ +#include + + +PATOMIC_EXPORT int +patomic_example_add( + int a, + int b +) +{ + return a + b; +}