Skip to content

Commit

Permalink
feat: hide symbols for shared libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
zchrissirhcz committed Nov 23, 2024
1 parent ba97f7b commit 3671f9d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rocbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,16 @@ function(rocbuild_set_debug_postfix TARGET)
endfunction()


function(rocbuild_hide_symbols TARGET)
get_target_property(TARGET_TYPE ${TARGET} TYPE)
if(TARGET_TYPE STREQUAL "SHARED_LIBRARY")
if((CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") OR
(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang"))
target_compile_options(${TARGET} PRIVATE "-fvisibility=hidden")
endif()
endif()
endfunction()


rocbuild_set_artifacts_path()
rocbuild_enable_ninja_colorful_output()
47 changes: 47 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,53 @@ def test_debug_postfix(self):
self.assertTrue(os.path.exists('build/debug_postfix/hello'))
shutil.rmtree('build/debug_postfix')

def test_hide_symbols(self):
if os_name == 'linux':
self.check_generate('hide_symbols', args='-DHIDDEN=1')
self.check_build('hide_symbols')
cmd = 'nm -C build/hide_symbols/libbar.so | grep " T "'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 1, lines)
self.assertTrue(lines[0].endswith(' T bar'))
shutil.rmtree('build/hide_symbols')

self.check_generate('hide_symbols', args='-DHIDDEN=0')
self.check_build('hide_symbols')
cmd = f'nm -C build/hide_symbols/libbar.so | grep " T "'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 2, lines)
self.assertTrue(lines[0].endswith(' T bar'))
self.assertTrue(lines[1].endswith(' T bar_internal'))
shutil.rmtree('build/hide_symbols')
elif os_name == 'mac':
self.check_generate('hide_symbols', args='-DHIDDEN=1')
self.check_build('hide_symbols')
cmd = 'nm -C build/hide_symbols/libbar.dylib | grep " T "'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 1, lines)
self.assertTrue(lines[0].endswith(' T _bar'))
shutil.rmtree('build/hide_symbols')

self.check_generate('hide_symbols', args='-DHIDDEN=0')
self.check_build('hide_symbols')
cmd = f'nm -C build/hide_symbols/libbar.dylib | grep " T "'
ret, out = check_output(cmd)
self.assertEqual(0, ret, out)
out = out.replace('\r\n', '\n')
lines = out.strip().split('\n')
self.assertEqual(len(lines), 2, lines)
self.assertTrue(lines[0].endswith(' T _bar'))
self.assertTrue(lines[1].endswith(' T _bar_internal'))
shutil.rmtree('build/hide_symbols')

if __name__ == "__main__":
unittest.main()
17 changes: 17 additions & 0 deletions tests/hide_symbols/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.10)
project(test_hide_symbols)

include(../../rocbuild.cmake)

add_library(bar SHARED
../src/bar.h
../src/bar.c
../src/bar_internal.h
../src/bar_internal.c
)
target_include_directories(bar PRIVATE ../src)
target_compile_definitions(bar PRIVATE BAR_EXPORTS)

if(HIDDEN)
rocbuild_hide_symbols(bar)
endif()
4 changes: 4 additions & 0 deletions tests/src/bar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "bar.h"
#include "bar_internal.h"

int bar() { return 1 + bar_internal(); }
25 changes: 25 additions & 0 deletions tests/src/bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#ifdef BAR_EXPORTS
# if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)
# define BAR_API __declspec(dllexport)
# elif defined(__GNUC__) && __GNUC__ >= 4
# define BAR_API __attribute__ ((visibility ("default")))
# endif
#else
# if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)
# define BAR_API __declspec(dllimport)
# else
# define BAR_API
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

BAR_API int bar();

#ifdef __cplusplus
}
#endif
6 changes: 6 additions & 0 deletions tests/src/bar_internal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "bar_internal.h"

int bar_internal()
{
return 2;
}
11 changes: 11 additions & 0 deletions tests/src/bar_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

int bar_internal();

#ifdef __cplusplus
}
#endif

0 comments on commit 3671f9d

Please sign in to comment.