8000 Add use of compile time option by ClausKlein · Pull Request #276 · aminya/project_options · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add use of compile time option #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions tests/myproj/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16...3.21)
cmake_minimum_required(VERSION 3.16...3.31)

# set a default CXX standard used by the external tools like clang-tidy, cppcheck, etc.
# You can later set fine-grained standards for each target using `target_compile_features`
Expand Down Expand Up @@ -49,6 +49,17 @@ if(NOT MSVC)
#message(STATUS "Detect Linker: ${LINKER}")
endif()

set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "ENABLE_INTERPROCEDURAL_OPTIMIZATION")
if(APPLE)
detect_macos_version(apple_version)
if(apple_version VERSION_GREATER_EQUAL 13)
# workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991
# although fixed, this problem still exists in github actions
#XXX add_link_options(-Wl,-ld_classic)
set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "")
endif()
endif()

# Initialize project_options
# uncomment the options to enable them
project_options(
Expand All @@ -60,7 +71,7 @@ project_options(
ENABLE_CLANG_TIDY
# ENABLE_INCLUDE_WHAT_YOU_USE
# ENABLE_GCC_ANALYZER
ENABLE_COVERAGE
#XXX ENABLE_COVERAGE
# ENABLE_PCH
# PCH_HEADERS
# ${PCH_HEADERS}
Expand Down Expand Up @@ -124,6 +135,13 @@ target_include_system_directories(
target_link_libraries(lib INTERFACE myproj_project_options myproj_project_warnings)
target_link_system_libraries(lib INTERFACE fmt::fmt Eigen3::Eigen)

option(LIB_WITH_EIGEN "Enable Eigen" OFF)
if(LIB_WITH_EIGEN)
target_compile_definitions(lib INTERFACE HAS_EIGEN)
endif()

target_link_libraries(main PRIVATE lib)

# Library
Comment on lines +138 to +144
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to export this option?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be passed from the builder of this library. project_options doesn't provide a specific mechanism for this.

add_library(lib2 "./src/mylib2/lib.cpp")
set(lib2_INCLUDE_DIR2 "include")
Expand Down Expand Up @@ -178,3 +196,6 @@ package_project(
)

package_project(NAME myproj_main TARGETS main)

set(CPACK_GENERATOR TGZ)
include(CPack)
18 changes: 14 additions & 4 deletions tests/myproj/include/mylib/lib.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

// test external pac
#include <Eigen/Dense>
#ifdef HAS_EIGEN
# include <Eigen/Dense>
#else
# include <vector>
#endif

#include <fmt/core.h>
#include <fmt/ranges.h>

Expand All @@ -17,14 +22,19 @@
#include <cstdint>
#include <cstring>

int some_fun() {
fmt::print("Hello from fmt{}", "!");
int some_fun()
{
fmt::print("Hello from lib{}\n", "!");

#ifdef HAS_EIGEN
// populate an Eigen vector with the values
auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1);
#else
auto eigen_vec = std::vector<int>() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
#endif

// print the vector
fmt::print("[{}]", fmt::join(eigen_vec, ", "));
fmt::print("[{}]\n", fmt::join(eigen_vec, ", "));

return 0;
}
23 changes: 15 additions & 8 deletions tests/myproj/src/main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "mylib/lib.hpp"

// test external pac
#include <Eigen/Dense>
#include <fmt/core.h>
#include <fmt/ranges.h>

#ifdef HAS_EIGEN
# include <Eigen/Dense>
#endif

// test std libraries
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

// test c libraries
#include <cassert>
Expand All @@ -15,21 +21,22 @@
#include <cstdint>
#include <cstring>

int main() {
fmt::print("Hello from fmt{}", "!");
int main()
{
fmt::print("Hello from main{}\n", "!");

Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3);
fmt::print("[{}]", fmt::join(eigen_vec, ", "));
auto eigen_vec = std::vector<int>() = {1, 2, 3};
fmt::print("[{}]\n", fmt::join(eigen_vec, ", "));

#if !defined(__MINGW32__) && !defined(__MSYS__)// TODO fails
#if defined(HAS_EIGEN) && !defined(__MINGW32__) && !defined(__MSYS__) // TODO fails
Eigen::VectorXd eigen_vec2 = Eigen::VectorXd::LinSpaced(10, 0, 1);
fmt::print("[{}]", fmt::join(eigen_vec2, ", "));
fmt::print("[{}]\n", fmt::join(eigen_vec2, ", "));
#endif

// trigger address sanitizer
// int *p = nullptr;
// *p = 1;

// trigger compiler warnings, clang-tidy, and cppcheck
int a;
int a = some_fun();
}
Loading
0