Skip to content

Commit

Permalink
GHI #11 Setup CMake for building project
Browse files Browse the repository at this point in the history
  • Loading branch information
doodspav committed Mar 11, 2023
1 parent 731469a commit f7643ad
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
73 changes: 73 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
)

# header files from /include
target_include_directories(
patomic_patomic ${warning_guard}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)

target_compile_features(patomic_patomic PUBLIC c_std_90)
30 changes: 30 additions & 0 deletions cmake/util/OptionVariables.cmake
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions cmake/util/PreventInSourceBuilds.cmake
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions cmake/util/ProjectIsTopLevel.cmake
Original file line number Diff line number Diff line change
@@ -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
)
22 changes: 22 additions & 0 deletions include/patomic/patomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PATOMIC_PATOMIC_H
#define PATOMIC_PATOMIC_H

#include <patomic/patomic_export.h>

#ifdef __cplusplus
extern "C" {
#endif


PATOMIC_EXPORT int
patomic_example_add(
int a,
int b
);


#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* PATOMIC_PATOMIC_H */
11 changes: 11 additions & 0 deletions src/patomic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <patomic/patomic.h>


PATOMIC_EXPORT int
patomic_example_add(
int a,
int b
)
{
return a + b;
}

0 comments on commit f7643ad

Please sign in to comment.