From d258001d366fd5ffa5f99a5dece6c6819b5f0e57 Mon Sep 17 00:00:00 2001 From: Maik Nijhuis <maik.nijhuis@triopsys.nl> Date: Thu, 24 Nov 2022 13:48:48 +0000 Subject: [PATCH] AST-1062 Search strategies in runtime library directory --- .gitlab-ci.yml | 3 ++- CMakeLists.txt | 21 ++++++++++++++++++++- aoluarunner/runner.cpp | 2 +- interface/aoflagger.cpp | 2 +- lua/telescopefile.cpp | 28 ++++++++++++++-------------- lua/telescopefile.h | 5 +---- rfigui/rfiguiwindow.cpp | 2 +- test/lua/telescopefiletest.cpp | 14 +++----------- 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a6a74b51..20236de0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -69,7 +69,8 @@ test: - mkdir build - cd build - cmake -DENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="-coverage" -DCMAKE_EXE_LINKER_FLAGS="-coverage" ../ - - make -j`nproc` all runtests install + # Do not install AOFlagger in this job. The tests should still run. + - make -j`nproc` all runtests - ctest artifacts: paths: diff --git a/CMakeLists.txt b/CMakeLists.txt index 14d3fc46..f459281a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -381,6 +381,25 @@ set(ALL_NON_GUI_FILES add_library(aoflagger-lib SHARED ${ALL_NON_GUI_FILES}) set_target_properties(aoflagger-lib PROPERTIES SOVERSION 0) set_target_properties(aoflagger-lib PROPERTIES OUTPUT_NAME aoflagger) +if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) + # By default, CMAKE_LIBRARY_OUTPUT_DIRECTORY is not defined. -> Store + # libaoflagger.so in lib/ and add the strategies in lib/aoflagger/strategies, + # so libaoflagger can find them. Using the current directory is not possible, + # since it already contains the 'aoflagger' binary, which has the same name as + # the 'aoflagger' directory. + file(MAKE_DIRECTORY lib/aoflagger) + file(COPY data/strategies DESTINATION lib/aoflagger) + set_target_properties(aoflagger-lib PROPERTIES LIBRARY_OUTPUT_DIRECTORY lib) +else() + # When CMAKE_LIBRARY_OUTPUT_DIRECTORY is set, ensure that libaoflagger.so can + # find the strategy files by adding them to that directory. (When building a + # python binary wheel, this copy is not necessary even though setup.py uses + # CMAKE_LIBRARY_OUTPUT_DIRECTORY: setup.py copies the original strategy files + # into the wheel.) + file(MAKE_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/aoflagger) + file(COPY data/strategies + DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/aoflagger) +endif() target_link_libraries(aoflagger-lib ${ALL_LIBRARIES}) # When building a docker image for an application that uses AOFlagger, like DP3, # the 'core_library' component allows installing only the core library using: @@ -609,7 +628,7 @@ if(ENABLE_TESTS) "${NAME}" PROPERTIES DEPENDS concatenate_frequency_integration_test_ms DEPENDS aoflagger ENVIRONMENT - PYTHONPATH=${CMAKE_BINARY_DIR}/python:${PYTHONPATH}) + PYTHONPATH=${CMAKE_BINARY_DIR}/python:$ENV{PYTHONPATH}) endforeach() endfunction() diff --git a/aoluarunner/runner.cpp b/aoluarunner/runner.cpp index b1be056d..184f0d60 100644 --- a/aoluarunner/runner.cpp +++ b/aoluarunner/runner.cpp @@ -90,7 +90,7 @@ void Runner::loadStrategy( << "using the rfigui and specify it with the '-strategy " "...' parameter.\n\n"; } - executeFilename = TelescopeFile::FindStrategy("", telescopeId); + executeFilename = TelescopeFile::FindStrategy(telescopeId); if (executeFilename.empty()) { throw std::runtime_error("Could not find a strategy for telescope " + TelescopeFile::TelescopeName(telescopeId) + diff --git a/interface/aoflagger.cpp b/interface/aoflagger.cpp index 3fcfc874..066d9838 100644 --- a/interface/aoflagger.cpp +++ b/interface/aoflagger.cpp @@ -9,7 +9,7 @@ namespace aoflagger { std::string AOFlagger::FindStrategyFile(enum TelescopeId telescopeId, const std::string& scenario) { return TelescopeFile::FindStrategy( - "", static_cast<TelescopeFile::TelescopeId>(telescopeId), scenario); + static_cast<TelescopeFile::TelescopeId>(telescopeId), scenario); } QualityStatistics AOFlagger::MakeQualityStatistics( diff --git a/lua/telescopefile.cpp b/lua/telescopefile.cpp index ea573114..7b4a9141 100644 --- a/lua/telescopefile.cpp +++ b/lua/telescopefile.cpp @@ -1,5 +1,10 @@ #include "telescopefile.h" +#include <dlfcn.h> +#include <filesystem> + +#include <boost/algorithm/string/case_conv.hpp> + #include "../imagesets/bhfitsimageset.h" #include "../imagesets/filterbankset.h" #include "../imagesets/fitsimageset.h" @@ -8,10 +13,6 @@ #include "../structures/msmetadata.h" -#include <boost/algorithm/string/case_conv.hpp> - -#include <filesystem> - #include <version.h> std::string TelescopeFile::TelescopeName( @@ -105,8 +106,7 @@ TelescopeFile::TelescopeId TelescopeFile::TelescopeIdFromName( return GENERIC_TELESCOPE; } -std::string TelescopeFile::FindStrategy(const std::string& argv0, - enum TelescopeId telescopeId, +std::string TelescopeFile::FindStrategy(enum TelescopeId telescopeId, const std::string& scenario) { std::string filename = boost::to_lower_copy(TelescopeName(telescopeId)); if (scenario.empty()) @@ -120,14 +120,14 @@ std::string TelescopeFile::FindStrategy(const std::string& argv0, "share/aoflagger/strategies" / filename; if (std::filesystem::exists(search)) return search.string(); - if (!argv0.empty()) { - const std::filesystem::path root = - std::filesystem::path(argv0).remove_filename(); - - search = root / "../share/aoflagger/strategies" / filename; - if (std::filesystem::exists(search)) return search.string(); - - search = root / "../data/strategies" / filename; + // Try using the directory of the AOFlagger library. + // When bundled as a python binary wheel, the strategies are there. + Dl_info dl_info; + if (dladdr(reinterpret_cast<const void*>(&TelescopeFile::FindStrategy), + &dl_info)) { + std::filesystem::path aoflagger_library_path(dl_info.dli_fname); + search = aoflagger_library_path.remove_filename() / "aoflagger/strategies" / + filename; if (std::filesystem::exists(search)) return search.string(); } diff --git a/lua/telescopefile.h b/lua/telescopefile.h index ce7ce3dc..738264e9 100644 --- a/lua/telescopefile.h +++ b/lua/telescopefile.h @@ -39,16 +39,13 @@ class TelescopeFile { /** * @brief Searches a strategy for a given telescope. * - * @param argv0 The argv[0] variable passed to main(), or empty if it is not - * available. * @param telescopeId One of the telescopes, if known, otherwise * GENERIC_TELESCOPE. * @param scenario Used as 'suffix' to the name of the telescope. This allows * multiple versions for the same telescope. * @returns Path to the strategy, or empty string if not found. */ - static std::string FindStrategy(const std::string& argv0, - enum TelescopeId telescopeId, + static std::string FindStrategy(enum TelescopeId telescopeId, const std::string& scenario = ""); static std::string TelescopeName(TelescopeFile::TelescopeId telescopeId); diff --git a/rfigui/rfiguiwindow.cpp b/rfigui/rfiguiwindow.cpp index 0123c3f6..bf34f435 100644 --- a/rfigui/rfiguiwindow.cpp +++ b/rfigui/rfiguiwindow.cpp @@ -1302,7 +1302,7 @@ void RFIGuiWindow::onStrategyOpenDefault(const std::string& name) { if (askToSaveChanges()) { const TelescopeFile::TelescopeId id = TelescopeFile::TelescopeIdFromName(name); - const std::string filename = TelescopeFile::FindStrategy("", id); + const std::string filename = TelescopeFile::FindStrategy(id); if (filename.empty()) showError("Could not find default strategy file for telescope '" + name + "' -- aoflagger is probably not installed properly"); diff --git a/test/lua/telescopefiletest.cpp b/test/lua/telescopefiletest.cpp index f914d514..8c139b02 100644 --- a/test/lua/telescopefiletest.cpp +++ b/test/lua/telescopefiletest.cpp @@ -8,28 +8,21 @@ BOOST_AUTO_TEST_SUITE(strategy_files, *boost::unit_test::label("lua")) BOOST_AUTO_TEST_CASE(find_strategy) { - std::filesystem::path argv0 = std::filesystem::current_path() / "aoflagger"; - auto all = TelescopeFile::List(); for (TelescopeFile::TelescopeId telescope : all) { - std::string path = - TelescopeFile::FindStrategy(argv0.string(), telescope, ""); + std::string path = TelescopeFile::FindStrategy(telescope); BOOST_CHECK_NE(path, ""); } } BOOST_AUTO_TEST_CASE(find_non_existing_strategy) { - std::filesystem::path argv0 = std::filesystem::current_path() / "aoflagger"; - std::string nonExisting = TelescopeFile::FindStrategy( - argv0.string(), TelescopeFile::GENERIC_TELESCOPE, "_NonExisting"); + TelescopeFile::GENERIC_TELESCOPE, "_NonExisting"); BOOST_CHECK_EQUAL(nonExisting, ""); } void runStrategy(TelescopeFile::TelescopeId telescope, const std::string& strategy_scenario = {}) { - std::filesystem::path argv0 = std::filesystem::current_path() / "aoflagger"; - aoflagger::AOFlagger flagger; const size_t pols = 8, width = 7, height = 10; aoflagger::ImageSet imageSet = flagger.MakeImageSet(width, height, pols); @@ -56,8 +49,7 @@ void runStrategy(TelescopeFile::TelescopeId telescope, imageSet.ImageBuffer(rfiP) + rfiY * imageSet.HorizontalStride() + rfiX; *rfiPixel = 1000.0; - std::string path = - TelescopeFile::FindStrategy(argv0.string(), telescope, strategy_scenario); + std::string path = TelescopeFile::FindStrategy(telescope, strategy_scenario); BOOST_CHECK(!path.empty()); aoflagger::Strategy strategy = flagger.LoadStrategyFile(path); aoflagger::FlagMask mask = strategy.Run(imageSet); -- GitLab