Skip to content

Commit

Permalink
[build] Set up symbol visibility for dart
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Apr 2, 2024
1 parent 5ba1e72 commit 82ee900
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 14 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ on:

jobs:
build:
name: win-${{ matrix.build_type }}
name: win-${{ matrix.build_type }}-shared_libs=${{ matrix.build_shared_libs }}
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
toolset: [""]
build_type: [Release]
build_shared_libs: [OFF] # TODO(JS): Add ON once shared lib build is resolved
build_shared_libs: [ON, OFF]

steps:
- name: Checkout
Expand Down Expand Up @@ -73,7 +73,6 @@ jobs:
-G "Visual Studio 17 2022" ^
-A x64 ^
-Wno-dev ${{ matrix.toolset }} ^
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} ^
-DCMAKE_TOOLCHAIN_FILE="${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake" ^
-DDART_MSVC_DEFAULT_OPTIONS=ON ^
-DDART_VERBOSE=ON ^
Expand Down
136 changes: 136 additions & 0 deletions cmake/dart_defs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,142 @@ function(dart_print_options)
message(STATUS "")
endfunction()

# cmake-format: off
# dart_check_compiler_visibility(<output_variable>)
#
# Macro to check for visibility capability in compiler
# cmake-format: on
macro(dart_check_compiler_visibility variable)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-fvisibility=hidden ${variable})
endmacro()

# dart_generate_export_header(
# TARGET_NAME <target_name>
# DESTINATION <dir_name>
# INCLUDE_DIR <dir_name>
# EXPORT_FILE_NAME <file_name>
# [BASE_NAME <base_name>]
# [EXPORT_ALL_SYMBOLS_BY_DEFAULT]
# )
#
# Function to create an export header for control of binary symbols visibility
#
function(dart_generate_export_header)
set(prefix _ARG)
set(options EXPORT_ALL_SYMBOLS_BY_DEFAULT)
set(oneValueArgs
TARGET_NAME
DESTINATION
EXPORT_FILE_NAME
BASE_NAME
BASE_DIR
)
set(multiValueArgs)
cmake_parse_arguments(
"${prefix}"
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)

# Check required argument
if(NOT _ARG_TARGET_NAME)
message(FATAL_ERROR "DEVELOPER ERROR: You must specify TARGET_NAME!")
return()
endif()
if(NOT _ARG_DESTINATION)
message(FATAL_ERROR "DEVELOPER ERROR: You must specify DESTINATION!")
return()
endif()
if(NOT _ARG_EXPORT_FILE_NAME)
message(FATAL_ERROR "DEVELOPER ERROR: You must specify EXPORT_FILE_NAME!")
return()
endif()

# Check if target is valid
if(NOT TARGET ${_ARG_TARGET_NAME})
message(
FATAL_ERROR
"DEVELOPER ERROR: Invalid target "
"\"${_ARG_TARGET_NAME}\" is passed! "
"Make sure this function is called after the target is defined by "
"add_library(<target> ...).")
return()
endif()

# Hide symbols by default
if(UNIX AND NOT _ARG_EXPORT_ALL_SYMBOLS_BY_DEFAULT)
dart_check_compiler_visibility(compiler_supports_visibility)
if(compiler_supports_visibility)
target_compile_options(${_ARG_TARGET_NAME} PRIVATE -fvisibility=hidden)
endif()
endif()

# Base name
if(_ARG_BASE_NAME)
set(base_name ${_ARG_BASE_NAME})
else()
set(base_name "${_ARG_TARGET_NAME}")
string(REPLACE "-" "_" base_name ${base_name})
endif()
string(TOUPPER ${base_name} base_name)

# Set up paths
set(export_file_path "${_ARG_DESTINATION}/${_ARG_EXPORT_FILE_NAME}")
set(export_detail_file_path "${_ARG_DESTINATION}/detail/${_ARG_EXPORT_FILE_NAME}")

# Generate CMake's default export header
include(GenerateExportHeader)
generate_export_header(
${_ARG_TARGET_NAME}
EXPORT_MACRO_NAME DETAIL_${base_name}_API
EXPORT_FILE_NAME ${export_detail_file_path}
DEPRECATED_MACRO_NAME _DART_DEPRECATED
)

# Generate final export header
file(
WRITE ${export_file_path}
"// This file is automatically generated by ${PROJECT_NAME}.\n"
"\n"
"#pragma once\n"
"\n"
"/**\n"
" * @brief Apply this macro to classes and functions that will need to be exposed\n"
" to the consumer libraries or programs.\n"
" */\n"
"#define ${base_name}_API \\\n"
" DETAIL_${base_name}_API\n"
"\n"
"#ifdef _MSC_VER\n"
" #define ${base_name}_TEMPL_INST_DECL_API\n"
"#else\n"
" #define ${base_name}_TEMPL_INST_DECL_API ${base_name}_API\n"
"#endif\n"
"\n"
"#ifdef _MSC_VER\n"
" #define ${base_name}_TEMPL_INST_DEF_API ${base_name}_API\n"
"#else\n"
" #define ${base_name}_TEMPL_INST_DEF_API\n"
"#endif\n"
"\n"
"#include \"detail/${_ARG_EXPORT_FILE_NAME}\"\n"
)

# Install generated export files
set(include_base_path ${CMAKE_INSTALL_INCLUDEDIR}/${org_name}/${project_name}${project_version_major})
set(export_install_path "${include_base_path}/${_ARG_BASE_DIR}")
set(detail_export_install_path "${export_install_path}/detail/")
install(FILES "${export_file_path}"
DESTINATION "${export_install_path}"
)
install(FILES "${export_detail_file_path}"
DESTINATION "${detail_export_install_path}"
)
endfunction()

function(dart_library)
set(prefix _ARG)
set(options
Expand Down
7 changes: 7 additions & 0 deletions dart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ get_property(dart_core_sources GLOBAL PROPERTY DART_CORE_SOURCES)

# Add target
dart_add_library(dart ${dart_core_headers} ${dart_core_sources})
dart_generate_export_header(
TARGET_NAME dart
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
EXPORT_FILE_NAME Export.hpp
BASE_NAME DART
BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
)
target_include_directories(dart BEFORE
PUBLIC
$<BUILD_INTERFACE:${DART_SOURCE_DIR}>
Expand Down
2 changes: 1 addition & 1 deletion dart/common/Aspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace common {

class Composite;

class Aspect
class DART_API Aspect
{
public:
friend class Composite;
Expand Down
2 changes: 1 addition & 1 deletion dart/common/CAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace dart::common {

/// A stateless memory allocator (in release mode) that uses std::malloc and
/// std::free for memory allocation and deallocation.
class CAllocator : public MemoryAllocator
class DART_API CAllocator : public MemoryAllocator
{
public:
/// Constructor
Expand Down
6 changes: 4 additions & 2 deletions dart/common/Console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#ifndef DART_COMMON_CONSOLE_HPP_
#define DART_COMMON_CONSOLE_HPP_

#include <dart/Export.hpp>

#include <ostream>
#include <string>

Expand All @@ -52,10 +54,10 @@ namespace dart {
namespace common {

/// \brief
std::ostream& colorMsg(const std::string& _msg, int _color);
DART_API std::ostream& colorMsg(const std::string& _msg, int _color);

/// \brief
std::ostream& colorErr(
DART_API std::ostream& colorErr(
const std::string& _msg,
const std::string& _file,
unsigned int _line,
Expand Down
2 changes: 1 addition & 1 deletion dart/common/MemoryAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
namespace dart::common {

/// Base class for std::allocator compatible allocators.
class MemoryAllocator : public Castable<MemoryAllocator>
class DART_API MemoryAllocator : public Castable<MemoryAllocator>
{
public:
/// Returns the default memory allocator
Expand Down
1 change: 1 addition & 0 deletions dart/common/Uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifndef DART_COMMON_URI_HPP_
#define DART_COMMON_URI_HPP_

#include <dart/Export.hpp>
#include <string>

namespace dart {
Expand Down
2 changes: 1 addition & 1 deletion dart/dynamics/detail/ShapeFrameAspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ShapeFrame;

namespace detail {

struct VisualAspectProperties
struct DART_API VisualAspectProperties
{
/// Color for the primitive shape
Eigen::Vector4d mRGBA;
Expand Down
2 changes: 1 addition & 1 deletion dart/external/odelcpsolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ file(GLOB srcs "*.cpp")
set(target_name ${PROJECT_NAME}-external-odelcpsolver)
set(component_name external-odelcpsolver)

dart_add_library(${target_name} ${hdrs} ${srcs})
dart_add_library(${target_name} STATIC ${hdrs} ${srcs})
target_include_directories(${target_name}
PUBLIC
$<BUILD_INTERFACE:${DART_SOURCE_DIR}>
Expand Down
4 changes: 3 additions & 1 deletion dart/optimizer/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#ifndef DART_OPTIMIZER_FUNCTION_HPP_
#define DART_OPTIMIZER_FUNCTION_HPP_

#include <dart/Export.hpp>

#include <Eigen/Dense>

#include <functional>
Expand All @@ -42,7 +44,7 @@
namespace dart {
namespace optimizer {

class Function
class DART_API Function
{
public:
/// Constructor
Expand Down
4 changes: 3 additions & 1 deletion dart/optimizer/Problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#include <dart/optimizer/Function.hpp>

#include <dart/Export.hpp>

#include <Eigen/Dense>

#include <vector>
Expand All @@ -45,7 +47,7 @@ namespace dart {
namespace optimizer {

/// \brief class Problem
class Problem
class DART_API Problem
{
public:
/// \brief Constructor
Expand Down
4 changes: 3 additions & 1 deletion dart/optimizer/Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#ifndef DART_OPTIMIZER_SOLVER_HPP_
#define DART_OPTIMIZER_SOLVER_HPP_

#include <dart/Export.hpp>

#include <Eigen/Dense>

#include <iostream>
Expand All @@ -49,7 +51,7 @@ class Problem;
/// problem types. This base class allows the different Solver implementations
/// to be swapped out with each other quickly and easily to help with testing,
/// benchmarking, and experimentation.
class Solver
class DART_API Solver
{
public:
/// The Solver::Properties class contains Solver parameters that are common
Expand Down
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ install-local = { cmd = "cmake --install build --prefix $CONDA_PREFIX", depends_
] }

configure-unix = { cmd = "cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release -DDART_VERBOSE=ON -DDART_USE_SYSTEM_IMGUI=ON" }
configure-win = { cmd = "cmake -S . -B build -G 'Visual Studio 17 2022' -DDART_VERBOSE=ON -DDART_MSVC_DEFAULT_OPTIONS=ON -DBUILD_SHARED_LIBS=OFF -DDART_USE_SYSTEM_IMGUI=OFF" }
configure-win = { cmd = "cmake -S . -B build -G 'Visual Studio 17 2022' -DDART_VERBOSE=ON -DDART_MSVC_DEFAULT_OPTIONS=ON -DBUILD_SHARED_LIBS=ON -DDART_USE_SYSTEM_IMGUI=OFF" }

example-hello-world = { cmd = "cmake --build build --target hello_world --parallel && ./build/bin/hello_world", depends_on = [
"configure",
Expand Down

0 comments on commit 82ee900

Please sign in to comment.