-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to enable binary relocatability in downstream libraries
Signed-off-by: Silvio Traversaro <[email protected]>
- Loading branch information
1 parent
9894b39
commit 8f89f08
Showing
6 changed files
with
347 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# For the tests, we make sure that the relative path in the build location is the same | ||
# of the install, and we make sure that the value returned by the shared library is ${CMAKE_BINARY_DIR} | ||
# while for the static library we make sure that the value returned is ${CMAKE_INSTALL_PREFIX} | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}") | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}") | ||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}") | ||
|
||
include(GzUtils) | ||
|
||
# dladdr from dl is a compulsory requirement for | ||
# gz_add_get_install_prefix_impl | ||
gz_find_package(DL REQUIRED) | ||
|
||
# shared test | ||
add_library(get-install-prefix-test-shared SHARED) | ||
set(PROJECT_LIBRARY_TARGET_NAME get-install-prefix-test-shared) | ||
|
||
gz_add_get_install_prefix_impl(GET_INSTALL_PREFIX_HEADER get_install_prefix_test_shared.h | ||
GET_INSTALL_PREFIX_FUNCTION gz::cmake::test::sharedlib::getInstallPrefix | ||
OVERRIDE_INSTALL_PREFIX_ENV_VARIABLE GET_INSTALL_PREFIX_TEST_INSTALL_PREFIX) | ||
set_target_properties(get-install-prefix-test-shared PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) | ||
target_include_directories(get-install-prefix-test-shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
|
||
# static test | ||
add_library(get-install-prefix-test-static STATIC) | ||
set(PROJECT_LIBRARY_TARGET_NAME get-install-prefix-test-static) | ||
|
||
gz_add_get_install_prefix_impl(GET_INSTALL_PREFIX_HEADER get_install_prefix_test_static.h | ||
GET_INSTALL_PREFIX_FUNCTION gz::cmake::test::staticlib::getInstallPrefix | ||
OVERRIDE_INSTALL_PREFIX_ENV_VARIABLE GET_INSTALL_PREFIX_TEST_INSTALL_PREFIX) | ||
target_include_directories(get-install-prefix-test-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
|
||
# Write header with CMake variables to check | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/get_install_prefix_test_cmake_variables.h | ||
"#pragma once | ||
#define CMAKE_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\" | ||
#define CMAKE_BINARY_DIR \"${CMAKE_BINARY_DIR}\" | ||
") | ||
|
||
|
||
|
||
# Add test executable | ||
add_executable(get_install_prefix_test get_install_prefix_test.cc) | ||
target_include_directories(get_install_prefix_test PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) | ||
target_link_libraries(get_install_prefix_test PRIVATE get-install-prefix-test-shared | ||
get-install-prefix-test-static) | ||
|
||
# Add the test only if GZ_ENABLE_RELOCATABLE_INSTALL is enabled, | ||
# as the test on gz_add_get_install_prefix_impl rely on GZ_ENABLE_RELOCATABLE_INSTALL | ||
# being enabled | ||
if(GZ_ENABLE_RELOCATABLE_INSTALL) | ||
add_test(NAME get_install_prefix_test COMMAND get_install_prefix_test) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2023 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
#include <filesystem> | ||
|
||
#include <get_install_prefix_test_shared.h> | ||
#include <get_install_prefix_test_static.h> | ||
#include <get_install_prefix_test_cmake_variables.h> | ||
|
||
std::string toCanonical(const std::string input_path) | ||
{ | ||
return std::filesystem::weakly_canonical(std::filesystem::path(input_path)).string(); | ||
} | ||
|
||
int main() | ||
{ | ||
// Test nominal behaviour | ||
|
||
std::string sharedInstallPrefix = gz::cmake::test::sharedlib::getInstallPrefix(); | ||
std::string staticInstallPrefix = gz::cmake::test::staticlib::getInstallPrefix(); | ||
|
||
std::cerr << "get-install-prefix test:" << std::endl; | ||
std::cerr << "sharedInstallPrefix: " << sharedInstallPrefix << std::endl; | ||
std::cerr << "CMAKE_BINARY_DIR: " << CMAKE_BINARY_DIR << std::endl; | ||
std::cerr << "staticInstallPrefix: " << staticInstallPrefix << std::endl; | ||
std::cerr << "CMAKE_INSTALL_PREFIX: " << CMAKE_INSTALL_PREFIX << std::endl; | ||
|
||
if (toCanonical(sharedInstallPrefix) != toCanonical(CMAKE_BINARY_DIR)) | ||
{ | ||
std::cerr << "getInstallPrefixShared returned unexpected value, test is failing." << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (toCanonical(staticInstallPrefix) != toCanonical(CMAKE_INSTALL_PREFIX)) | ||
{ | ||
std::cerr << "getInstallPrefixStatic returned unexpected value, test is failing." << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// Test behaviour after setting the environment variable to modify the return values (only on Unix so we can use setenv) | ||
#ifndef _WIN32 | ||
std::string overrideValue = "test_override_value"; | ||
int overwrite = 1; | ||
setenv("GET_INSTALL_PREFIX_TEST_INSTALL_PREFIX" , overrideValue.c_str(), overwrite); | ||
std::string sharedInstallPrefixWithOverride = gz::cmake::test::sharedlib::getInstallPrefix(); | ||
std::string staticInstallPrefixWithOverride = gz::cmake::test::staticlib::getInstallPrefix(); | ||
|
||
std::cerr << "overrideValue: " << overrideValue << std::endl; | ||
std::cerr << "sharedInstallPrefixWithOverride: " << sharedInstallPrefixWithOverride << std::endl; | ||
std::cerr << "staticInstallPrefixWithOverride: " << staticInstallPrefixWithOverride << std::endl; | ||
|
||
if (overrideValue != sharedInstallPrefixWithOverride) | ||
{ | ||
std::cerr << "getInstallPrefixShared with env variable override returned unexpected value, test is failing." << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (overrideValue != sharedInstallPrefixWithOverride) | ||
{ | ||
std::cerr << "getInstallPrefixShared with env variable override returned unexpected value, test is failing." << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
#endif | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace gz | ||
{ | ||
namespace cmake | ||
{ | ||
namespace test | ||
{ | ||
namespace sharedlib | ||
{ | ||
std::string getInstallPrefix(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace gz | ||
{ | ||
namespace cmake | ||
{ | ||
namespace test | ||
{ | ||
namespace staticlib | ||
{ | ||
std::string getInstallPrefix(); | ||
} | ||
} | ||
} | ||
} |