mdml-cgi/cmake/BuildProperties.cmake

117 lines
3.6 KiB
CMake

# BuildProperties.cmake
# Copyright (c) 2024 Saul D Beniquez
# License: MIT
#
# This module defines a function prevent_in_source_build() that prevents in-source builds
# and sets a policy for CMake version 3.24.0 and above.
function(prevent_in_source_build)
# Prevent in-source builds
if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Source and build directories cannot be the same.")
endif()
endfunction()
function(set_artifact_dir path)
set(ARTIFACT_DIR ${path})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/lib PARENT_SCOPE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/lib PARENT_SCOPE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ARTIFACT_DIR}/bin PARENT_SCOPE)
endfunction()
function(disable_deprecated_features)
# Use new timestamp behavior when extracting files archives
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()
endfunction()
function(disable_tests_if_subproject)
if (NOT DEFINED PROJECT_NAME)
option(BUILD_TESTING "Build and run unit tests" ON)
else()
option(BUILD_TESTING "Build and run unit tests" OFF)
endif()
endfunction()
function(include_usr_local)
option(LOCAL_INCLUDES "Automatically add /usr/local/include to include search paths" ON)
option(LOCAL_LIBS "Automatically add /usr/local/lib to linker search paths" ON)
if (LOCAL_INCLUDES)
if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
include_directories(BEFORE SYSTEM /usr/local/include)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
include_directories(BEFORE SYSTEM /opt/homebrew/include)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
include_directories(BEFORE SYSTEM /usr/local/include)
endif()
endif()
if (LOCAL_LIBS)
if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
link_directories(BEFORE /usr/local/lib)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
link_directories(BEFORE /opt/homebrew/lib)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
link_directories(BEFORE /usr/local/lib)
endif()
endif()
endfunction()
function(add_data_target)
set(options "")
set(oneValueArgs NAME PATH DESTINATION)
set(multiValueArgs "")
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Assign positional arguments if NAME, PATH, and DESTINATION were not named explicitly
list(LENGTH ARGN arg_len)
if(arg_len EQUAL 3)
list(GET ARGN 0 ARG_NAME)
list(GET ARGN 1 ARG_PATH)
list(GET ARGN 2 ARG_DESTINATION)
elseif(arg_len EQUAL 2)
list(GET ARGN 0 ARG_PATH)
list(GET ARGN 1 ARG_DESTINATION)
endif()
# Generate the target name
if(ARG_NAME)
set(target_name "copy_${ARG_NAME}")
else()
get_filename_component(filename "${ARG_PATH}" NAME_WE)
set(target_name "copy_${filename}")
endif()
# Create a custom target for the copy operation
add_custom_target(${target_name} ALL
COMMENT "Copying ${ARG_PATH} to ${ARG_DESTINATION}"
)
if (IS_DIRECTORY "${ARG_PATH}")
# Add a custom command to actually perform the file copy
add_custom_command(
TARGET ${target_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory "${ARG_PATH}" "${ARG_DESTINATION}"
DEPENDS "${ARG_PATH}"
COMMENT "Copied ${ARG_PATH} to ${ARG_DESTINATION}"
)
else()
add_custom_command(
TARGET ${target_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${ARG_PATH}" "${ARG_DESTINATION}"
DEPENDS "${ARG_PATH}"
COMMENT "Copied ${ARG_PATH} to ${ARG_DESTINATION}"
)
endif()
endfunction()
# vim: ts=4 sts=4 sw=4 noet :