Skip to content

Commit

Permalink
Static libstdcxx and python (#139)
Browse files Browse the repository at this point in the history
Support python + static libstdc++
  • Loading branch information
jrmadsen authored Aug 28, 2022
1 parent 0dd8f52 commit a1dcd1b
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 57 deletions.
34 changes: 27 additions & 7 deletions cmake/BuildSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ omnitrace_add_option(OMNITRACE_BUILD_STATIC_LIBGCC
"Build with -static-libgcc if possible" OFF)
omnitrace_add_option(OMNITRACE_BUILD_STATIC_LIBSTDCXX
"Build with -static-libstdc++ if possible" OFF)
omnitrace_add_option(OMNITRACE_BUILD_STACK_PROTECTOR "Build with -fstack-protector" ON)

omnitrace_add_interface_library(omnitrace-static-libgcc
"Link to static version of libgcc")
omnitrace_add_interface_library(omnitrace-static-libstdcxx
"Link to static version of libstdc++")
omnitrace_add_interface_library(omnitrace-static-libgcc-optional
"Link to static version of libgcc")
omnitrace_add_interface_library(omnitrace-static-libstdcxx-optional
"Link to static version of libstdc++")

target_compile_definitions(omnitrace-compile-options INTERFACE $<$<CONFIG:DEBUG>:DEBUG>)

Expand Down Expand Up @@ -194,6 +199,18 @@ if(OMNITRACE_USE_COMPILE_TIMING)
target_link_libraries(omnitrace-compile-options INTERFACE omnitrace-compile-timing)
endif()

# ----------------------------------------------------------------------------------------#
# fstack-protector
#
omnitrace_add_interface_library(omnitrace-stack-protector
"Adds stack-protector compiler flags")
add_target_flag_if_avail(omnitrace-stack-protector "-fstack-protector-strong"
"-Wstack-protector")

if(OMNITRACE_BUILD_STACK_PROTECTOR)
target_link_libraries(omnitrace-compile-options INTERFACE omnitrace-stack-protector)
endif()

# ----------------------------------------------------------------------------------------#
# developer build flags
#
Expand Down Expand Up @@ -281,13 +298,6 @@ else()
set(OMNITRACE_USE_SANITIZER OFF)
endif()

if(MSVC)
# VTune is much more helpful when debug information is included in the generated
# release code.
add_flag_if_avail("/Zi")
add_flag_if_avail("/DEBUG")
endif()

# ----------------------------------------------------------------------------------------#
# static lib flags
#
Expand All @@ -308,6 +318,16 @@ target_link_options(
omnitrace-static-libstdcxx INTERFACE
$<$<COMPILE_LANGUAGE:CXX>:$<$<CXX_COMPILER_ID:GNU,Clang>:-static-libstdc++>>)

if(OMNITRACE_BUILD_STATIC_LIBGCC)
target_link_libraries(omnitrace-static-libgcc-optional
INTERFACE omnitrace-static-libgcc)
endif()

if(OMNITRACE_BUILD_STATIC_LIBSTDCXX)
target_link_libraries(omnitrace-static-libstdcxx-optional
INTERFACE omnitrace-static-libstdcxx)
endif()

# ----------------------------------------------------------------------------------------#
# user customization
#
Expand Down
60 changes: 46 additions & 14 deletions cmake/Formatting.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ find_program(OMNITRACE_CLANG_FORMAT_EXE NAMES clang-format-11 clang-format-mp-11
clang-format)

find_program(OMNITRACE_CMAKE_FORMAT_EXE NAMES cmake-format)
find_program(OMNITRACE_BLACK_FORMAT_EXE NAMES black)

if(OMNITRACE_CLANG_FORMAT_EXE)
add_custom_target(format-omnitrace)
if(NOT TARGET format)
add_custom_target(format)
endif()
foreach(_TYPE source python cmake)
if(NOT TARGET format-${_TYPE})
add_custom_target(format-${_TYPE})
endif()
endforeach()

if(OMNITRACE_CLANG_FORMAT_EXE
OR OMNITRACE_BLACK_FORMAT_EXE
OR OMNITRACE_CMAKE_FORMAT_EXE)
file(GLOB_RECURSE sources ${PROJECT_SOURCE_DIR}/source/*.cpp)
file(GLOB_RECURSE headers ${PROJECT_SOURCE_DIR}/source/*.hpp
${PROJECT_SOURCE_DIR}/source/*.hpp.in ${PROJECT_SOURCE_DIR}/source/*.h
Expand All @@ -63,16 +76,25 @@ if(OMNITRACE_CLANG_FORMAT_EXE)
list(REMOVE_ITEM examples ${external})
list(REMOVE_ITEM cmake_files ${external})
endif()
add_custom_target(
format-omnitrace-source
${OMNITRACE_CLANG_FORMAT_EXE} -i ${sources} ${headers} ${examples}
COMMENT "[omnitrace] Running C++ formatter ${OMNITRACE_CLANG_FORMAT_EXE}...")
add_custom_target(format-omnitrace)
add_dependencies(format-omnitrace format-omnitrace-source)
if(NOT TARGET format)
add_custom_target(format)

if(OMNITRACE_CLANG_FORMAT_EXE)
add_custom_target(
format-omnitrace-source
${OMNITRACE_CLANG_FORMAT_EXE} -i ${sources} ${headers} ${examples}
COMMENT "[omnitrace] Running C++ formatter ${OMNITRACE_CLANG_FORMAT_EXE}...")
endif()

if(OMNITRACE_BLACK_FORMAT_EXE)
add_custom_target(
format-omnitrace-python
${OMNITRACE_BLACK_FORMAT_EXE} ${PROJECT_SOURCE_DIR}
COMMENT
"[omnitrace] Running Python formatter ${OMNITRACE_BLACK_FORMAT_EXE}...")
if(NOT TARGET format-python)
add_custom_target(format-python)
endif()
endif()
add_dependencies(format format-omnitrace)

if(OMNITRACE_CMAKE_FORMAT_EXE)
add_custom_target(
format-omnitrace-cmake
Expand All @@ -82,10 +104,20 @@ if(OMNITRACE_CLANG_FORMAT_EXE)
if(NOT TARGET format-cmake)
add_custom_target(format-cmake)
endif()
add_dependencies(format-cmake format-omnitrace-cmake)
endif()

foreach(_TYPE source python cmake)
if(TARGET format-omnitrace-${_TYPE})
add_dependencies(format-omnitrace format-omnitrace-${_TYPE})
add_dependencies(format-${_TYPE} format-omnitrace-${_TYPE})
endif()
endforeach()

foreach(_TYPE source python)
if(TARGET format-omnitrace-${_TYPE})
add_dependencies(format format-omnitrace-${_TYPE})
endif()
endforeach()
else()
message(
AUTHOR_WARNING
"clang-format could not be found. format build target not available.")
message(STATUS "clang-format could not be found. format build target not available.")
endif()
5 changes: 5 additions & 0 deletions cmake/Packages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ endif()
target_compile_definitions(omnitrace-timemory-config INTERFACE TIMEMORY_PAPI_ARRAY_SIZE=16
TIMEMORY_USE_ROOFLINE=0)

if(OMNITRACE_BUILD_STACK_PROTECTOR)
add_target_flag_if_avail(omnitrace-timemory-config "-fstack-protector-strong"
"-Wstack-protector")
endif()

set(TIMEMORY_EXTERNAL_INTERFACE_LIBRARY
omnitrace-timemory-config
CACHE STRING "timemory configuration interface library")
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
: ${LTO:="OFF"}
: ${STRIP:="ON"}
: ${LIBGCC:="ON"}
: ${LIBSTDCXX:="OFF"}
: ${LIBSTDCXX:="ON"}
: ${MAX_THREADS:=2048}
: ${PERFETTO_TOOLS:="ON"}
: ${HIDDEN_VIZ:="ON"}
Expand Down
11 changes: 4 additions & 7 deletions source/bin/omnitrace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)

add_executable(omnitrace-exe ${_EXCLUDE})
add_executable(omnitrace-exe)

target_sources(
omnitrace-exe
Expand All @@ -22,12 +22,9 @@ target_sources(

target_link_libraries(
omnitrace-exe
PRIVATE omnitrace::omnitrace-headers
omnitrace::omnitrace-dyninst
omnitrace::omnitrace-compile-options
omnitrace::omnitrace-compile-definitions
timemory::timemory-headers
$<IF:$<BOOL:${OMNITRACE_USE_SANITIZER}>,omnitrace::omnitrace-sanitizer,>)
PRIVATE omnitrace::omnitrace-headers omnitrace::omnitrace-dyninst
omnitrace::omnitrace-compile-options omnitrace::omnitrace-compile-definitions
omnitrace::omnitrace-sanitizer timemory::timemory-headers)

set_target_properties(
omnitrace-exe
Expand Down
9 changes: 2 additions & 7 deletions source/lib/omnitrace-dl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ target_include_directories(
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(
omnitrace-dl-library
PUBLIC
${dl_LIBRARY}
$<BUILD_INTERFACE:omnitrace::common-library>
$<BUILD_INTERFACE:omnitrace::omnitrace-compile-definitions>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_STATIC_LIBGCC}>,omnitrace::omnitrace-static-libgcc,>>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_STATIC_LIBSTDCXX}>,omnitrace::omnitrace-static-libstdcxx,>>
)
PUBLIC $<BUILD_INTERFACE:${dl_LIBRARY}> $<BUILD_INTERFACE:omnitrace::common-library>
$<BUILD_INTERFACE:omnitrace::omnitrace-compile-definitions>)

add_target_cxx_flag_if_avail(omnitrace-dl-library "-ftls-model=global-dynamic")
add_target_cxx_flag_if_avail(omnitrace-dl-library "-g")
Expand Down
9 changes: 4 additions & 5 deletions source/lib/omnitrace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ target_link_libraries(
$<BUILD_INTERFACE:omnitrace::omnitrace-rocprofiler>
$<BUILD_INTERFACE:omnitrace::omnitrace-rocm-smi>
$<BUILD_INTERFACE:omnitrace::omnitrace-rccl>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_LTO}>,omnitrace::omnitrace-lto,>>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_STATIC_LIBGCC}>,omnitrace::omnitrace-static-libgcc,>>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_STATIC_LIBSTDCXX}>,omnitrace::omnitrace-static-libstdcxx,>>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_USE_SANITIZER}>,omnitrace::omnitrace-sanitizer,>>
)
$<BUILD_INTERFACE:omnitrace::omnitrace-static-libgcc-optional>
$<BUILD_INTERFACE:omnitrace::omnitrace-static-libstdcxx-optional>
$<BUILD_INTERFACE:omnitrace::omnitrace-sanitizer>
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_LTO}>,omnitrace::omnitrace-lto,>>)

# ------------------------------------------------------------------------------#
#
Expand Down
26 changes: 12 additions & 14 deletions source/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# ########################################################################################

if(OMNITRACE_BUILD_STATIC_LIBSTDCXX)
omnitrace_message(FATAL_ERROR
"static libstdc++ is not compatible with python bindings")
# omnitrace_message(FATAL_ERROR "static libstdc++ is not compatible with python
# bindings")
endif()

# if set, will screw up loading library
Expand Down Expand Up @@ -76,18 +76,16 @@ set(pybind_libs pybind11::module)
add_library(libpyomnitrace-interface INTERFACE)
target_link_libraries(
libpyomnitrace-interface
INTERFACE
pybind11::module
timemory::timemory-headers
omnitrace::omnitrace-headers
omnitrace::omnitrace-compile-options
omnitrace::omnitrace-lto
omnitrace::omnitrace-dl-library
omnitrace::omnitrace-user-library
omnitrace::omnitrace-python
omnitrace::omnitrace-python-compile-options
$<BUILD_INTERFACE:$<IF:$<BOOL:${OMNITRACE_BUILD_STATIC_LIBGCC}>,omnitrace::omnitrace-static-libgcc,>>
)
INTERFACE pybind11::module
timemory::timemory-headers
omnitrace::omnitrace-headers
omnitrace::omnitrace-compile-options
omnitrace::omnitrace-static-libgcc-optional
omnitrace::omnitrace-lto
omnitrace::omnitrace-dl-library
omnitrace::omnitrace-user-library
omnitrace::omnitrace-python
omnitrace::omnitrace-python-compile-options)

omnitrace_target_compile_definitions(libpyomnitrace-interface
INTERFACE OMNITRACE_PYBIND11_SOURCE)
Expand Down
20 changes: 19 additions & 1 deletion source/python/libpyomnitrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ PYBIND11_MODULE(libpyomnitrace, omni)
{
using namespace pyomnitrace;

py::doc("Omnitrace Python bindings for profiling, user API, and code coverage "
"post-processing");

static bool _is_initialized = false;
static bool _is_finalized = false;
static auto _get_use_mpi = []() {
Expand Down Expand Up @@ -162,10 +165,25 @@ PYBIND11_MODULE(libpyomnitrace, omni)
},
"Finalize omnitrace");

py::doc("omnitrace profiler for python");
pyprofile::generate(omni);
pycoverage::generate(omni);
pyuser::generate(omni);

auto _python_path = tim::get_env("OMNITRACE_PATH", std::string{}, false);
auto _libpath = std::string{ "libomnitrace-dl.so" };
if(!_python_path.empty()) _libpath = TIMEMORY_JOIN("/", _python_path, _libpath);
// permit env override if default path fails/is wrong
_libpath = tim::get_env("OMNITRACE_DL_LIBRARY", _libpath);
// this is necessary when building with -static-libstdc++
// without it, loading libomnitrace.so within libomnitrace-dl.so segfaults
if(!dlopen(_libpath.c_str(), RTLD_NOW | RTLD_GLOBAL))
{
auto _msg =
TIMEMORY_JOIN("", "dlopen(\"", _libpath, "\", RTLD_NOW | RTLD_GLOBAL)");
perror(_msg.c_str());
fprintf(stderr, "[omnitrace][dl][pid=%i] %s :: %s\n", getpid(), _msg.c_str(),
dlerror());
}
}

//======================================================================================//
Expand Down
6 changes: 6 additions & 0 deletions source/python/omnitrace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
"""

try:
import os

os.environ["OMNITRACE_PATH"] = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../..")
)

from .libpyomnitrace import coverage
from . import user
from .profiler import Profiler, FakeProfiler
Expand Down
2 changes: 1 addition & 1 deletion source/python/omnitrace/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def stop(self):
_count = Region._counter
self._active = False
if _count != self._count:
raise LogicError(
raise RuntimeError(
f"{self._label} was not popped in the order it was pushed. Current stack number: {_count}, expected stack number: {self._count}"
)
_libuser.pop_region(self._label)
Expand Down

0 comments on commit a1dcd1b

Please sign in to comment.