Skip to content
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

Add tracy.cmake #712

Merged
merged 8 commits into from
Sep 11, 2024
Merged
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
107 changes: 107 additions & 0 deletions tracy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (C) 2024 INRIA.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

# .rst:
# ~~~
# .. command:: GENERATE_TRACY_HEADER (
# INCLUDE_DIR <include_dir>
# HEADER_DIR <header_dir>
# FILENAME <filename>
# LIBRARY_NAME <library_name>
# TRACY_ENABLE <tracy_enable>)
# ~~~
#
# This function generates a tracy wrapper header at
# ``<include_dir>/<header_dir>/<filename>``.
#
# If INSTALL_GENERATED_HEADERS is ON, the configuration header will be installed
# in
# ``${CMAKE_INSTALL_INCLUDEDIR}/<header_dir>``.
#
# :param INCLUDE_DIR: Include root directory (absolute).
#
# :param HEADER_DIR: Include sub directory.
#
# :param FILENAME: Configuration header name.
#
# :param LIBRARY_NAME: Define prefix, should match the compiled library name.
#
# :param TRACY_ENABLE: Controls if tracy macro are empty or call tracy.
function(_GENERATE_TRACY_HEADER)
set(options)
set(oneValueArgs INCLUDE_DIR HEADER_DIR FILENAME LIBRARY_NAME TRACY_ENABLE)
set(multiValueArgs)
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}"
${ARGN})

# Set variables for configure_file command
set(LIBRARY_NAME ${ARGS_LIBRARY_NAME})
# Activate/Deactivate Tracy macro definition
if(ARGS_TRACY_ENABLE)
set(DEFINE_TRACY_ENABLE "#define ${LIBRARY_NAME}_TRACY_ENABLE")
else()
set(DEFINE_TRACY_ENABLE)
endif()

configure_file(${PROJECT_JRL_CMAKE_MODULE_DIR}/tracy.hh.cmake
${ARGS_INCLUDE_DIR}/${ARGS_HEADER_DIR}/${ARGS_FILENAME} @ONLY)

# Install it if requested.
if(INSTALL_GENERATED_HEADERS)
install(
FILES ${ARGS_INCLUDE_DIR}/${ARGS_HEADER_DIR}/${ARGS_FILENAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${ARGS_HEADER_DIR}
PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_WRITE)
endif()
endfunction()

# .rst:
#
# Read project parameters to generate tracy header
function(_SETUP_TRACY_HEADER)
# Install project headers.
if(DEFINED PROJECT_CUSTOM_HEADER_DIR)
set(HEADER_DIR "${PROJECT_CUSTOM_HEADER_DIR}")
elseif(DEFINED CUSTOM_HEADER_DIR)
set(HEADER_DIR "${CUSTOM_HEADER_DIR}")
else()
string(REGEX REPLACE "[^a-zA-Z0-9]" "/" HEADER_DIR "${PROJECT_NAME}")
endif()
string(TOLOWER "${HEADER_DIR}" "HEADER_DIR")

if(NOT DEFINED PROJECT_CUSTOM_HEADER_EXTENSION)
set(HEADER_EXTENSION "hh")
else()
set(HEADER_EXTENSION ${PROJECT_CUSTOM_HEADER_EXTENSION})
endif()

string(REGEX REPLACE "[^a-zA-Z0-9]" "_" PACKAGE_CPPNAME "${PROJECT_NAME}")
string(TOUPPER "${PACKAGE_CPPNAME}" PACKAGE_CPPNAME)

_generate_tracy_header(
INCLUDE_DIR
${PROJECT_BINARY_DIR}/include
HEADER_DIR
${HEADER_DIR}
FILENAME
tracy.${HEADER_EXTENSION}
LIBRARY_NAME
${PACKAGE_CPPNAME}
TRACY_ENABLE
${${PACKAGE_CPPNAME}_TRACY_ENABLE})
endfunction()

option(${PACKAGE_CPPNAME}_TRACY_ENABLE "Enable Tracy." OFF)
_setup_tracy_header()
33 changes: 33 additions & 0 deletions tracy.hh.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file has been automatically generated by the jrl-cmakemodules.
* Please see
* https://github.com/jrl-umi3218/jrl-cmakemodules/blob/master/tracy.hh.cmake for
* details.
*/

#ifndef @LIBRARY_NAME@_TRACY_HPP
#define @LIBRARY_NAME@_TRACY_HPP

@DEFINE_TRACY_ENABLE@

#ifdef @LIBRARY_NAME@_TRACY_ENABLE
#if defined(__clang__) || defined(__GNUC__)
#define TracyFunction __PRETTY_FUNCTION__
#elif defined(_MSC_VER)
#define TracyFunction __FUNCSIG__
#endif
#include <tracy/Tracy.hpp>
#define @LIBRARY_NAME@_TRACY_ZONE_SCOPED_N(name) ZoneScopedN(name)
#define @LIBRARY_NAME@_TRACY_ZONE_SCOPED ZoneScoped
#define @LIBRARY_NAME@_TRACY_ZONE_NAMED_N(varname, name, active) ZoneNamedN(varname, name, active)
#define @LIBRARY_NAME@_TRACY_ZONE_NAMED(varname, active) ZoneNamed(varname, active)
#define @LIBRARY_NAME@_TRACY_SET_THREAD_NAME(name) tracy::SetThreadName(name)
#else
#define @LIBRARY_NAME@_TRACY_ZONE_SCOPED_N(x)
#define @LIBRARY_NAME@_TRACY_ZONE_SCOPED
#define @LIBRARY_NAME@_TRACY_ZONE_NAMED_N(x, y, z)
#define @LIBRARY_NAME@_TRACY_ZONE_NAMED(x, y)
#define @LIBRARY_NAME@_TRACY_SET_THREAD_NAME(x)
#endif

#endif // @LIBRARY_NAME@_TRACY_HPP
Loading