From bb5258c17ab05ce1a36200cbf3795d4d41d72a99 Mon Sep 17 00:00:00 2001 From: Christopher Lee Date: Tue, 24 Jan 2023 12:24:53 -0700 Subject: [PATCH] Static library fixes (#143) --- CHANGELOG.md | 3 +++ CMakeLists.txt | 16 ++++++++++++---- cpp/CMakeLists.txt | 10 +++++----- cpp/include/copc-lib/las/vlr.hpp | 3 +++ cpp/src/io/base_reader.cpp | 5 +++-- cpp/src/io/copc_reader.cpp | 6 +++--- cpp/src/io/copc_writer_internal.cpp | 4 ++-- cpp/src/io/laz_base_writer.cpp | 4 ++-- python/CMakeLists.txt | 8 ++++---- 9 files changed, 37 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c91be2c7..e02c153a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- **\[C++\]** Allow for building as static library with lazperf built externally + ## [2.5.3] - 2022-10-27 ### Fixed diff --git a/CMakeLists.txt b/CMakeLists.txt index d43ed46c..f229b6a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,13 +95,21 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/libs/laz-perf") include_directories(libs/laz-perf/cpp) set(WITH_TESTS ${WITH_TEST_TEMP}) set(LAZPERF_LIB_NAME "lazperf") - set(LAZPERF_IS_FOUND ON) set(EXTRA_EXPORT_TARGETS "lazperf") else() # if not, assume lazperf is installed on the system - find_package(LAZPERF ${LAZPERF_VERSION} REQUIRED) - set(LAZPERF_LIB_NAME "LAZPERF::lazperf") - set(EXTRA_EXPORT_TARGETS "") + find_package(LAZPERF ${LAZPERF_VERSION}) + if (LAZPERF_FOUND) + message("Using system-wide laz-perf") + # lazperf installed system-wide + set(LAZPERF_LIB_NAME "LAZPERF::lazperf") + set(EXTRA_EXPORT_TARGETS "") + else () + # assume lazperf is compiled in tandem + message("Lazperf package not found, including ${LAZPERF_DIR}/cpp") + set(LAZPERF_LIB_NAME "lazperf") + set(EXTRA_EXPORT_TARGETS "lazperf") + endif () endif() # Enable RPATH support for installed binaries and libraries diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 5ace0d67..c521cd18 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -56,16 +56,16 @@ set(${LIBRARY_TARGET_NAME}_SRC ) # Compile static library for pip wheels -if (WITH_PYTHON) +if (WITH_PYTHON OR NOT BUILD_SHARED_LIBS) add_library(${LIBRARY_TARGET_NAME}-s STATIC ${${LIBRARY_TARGET_NAME}_SRC} ${${LIBRARY_TARGET_NAME}_HDR}) set_target_properties(${LIBRARY_TARGET_NAME}-s PROPERTIES VERSION ${${PROJECT_NAME}_VERSION}) target_include_directories(${LIBRARY_TARGET_NAME}-s PUBLIC "$" "$/${CMAKE_INSTALL_INCLUDEDIR}>") - if (LAZPERF_IS_FOUND) - target_link_libraries(${LIBRARY_TARGET_NAME}-s PRIVATE lazperf_s) - else() + if (LAZPERF_FOUND) target_link_libraries(${LIBRARY_TARGET_NAME}-s PRIVATE ${LAZPERF_LIB_NAME}) - endif() + else () + target_link_libraries(${LIBRARY_TARGET_NAME}-s PRIVATE lazperf_s) + endif () message(STATUS "Created target ${LIBRARY_TARGET_NAME}-s for export ${PROJECT_NAME}.") endif() diff --git a/cpp/include/copc-lib/las/vlr.hpp b/cpp/include/copc-lib/las/vlr.hpp index e0a46b7c..363e5951 100644 --- a/cpp/include/copc-lib/las/vlr.hpp +++ b/cpp/include/copc-lib/las/vlr.hpp @@ -13,6 +13,9 @@ namespace copc::las using WktVlr = lazperf::wkt_vlr; using EbVlr = lazperf::eb_vlr; +static const int VLR_HEADER_SIZE = 54; +static const int EVLR_HEADER_SIZE = 60; + // Gets the sum of the byte size the extra bytes will take up, for calculating point_record_len int NumBytesFromExtraBytes(const std::vector &items); diff --git a/cpp/src/io/base_reader.cpp b/cpp/src/io/base_reader.cpp index 8312e838..9a5d2f1c 100644 --- a/cpp/src/io/base_reader.cpp +++ b/cpp/src/io/base_reader.cpp @@ -7,6 +7,7 @@ #include "copc-lib/copc/copc_config.hpp" #include "copc-lib/copc/extents.hpp" #include "copc-lib/las/header.hpp" +#include "copc-lib/las/vlr.hpp" #include #include @@ -67,7 +68,7 @@ las::WktVlr BaseReader::ReadWktVlr(std::map &vlrs) auto offset = FetchVlr(vlrs, "LASF_Projection", 2112); if (offset != 0) { - in_stream_->seekg(offset + lazperf::evlr_header::Size); + in_stream_->seekg(offset + las::EVLR_HEADER_SIZE); return las::WktVlr::create(*in_stream_, static_cast(vlrs[offset].data_length)); } return las::WktVlr(); @@ -78,7 +79,7 @@ las::EbVlr BaseReader::ReadExtraBytesVlr(std::map &vlr auto offset = FetchVlr(vlrs, "LASF_Spec", 4); if (offset != 0) { - in_stream_->seekg(offset + lazperf::vlr_header::Size); + in_stream_->seekg(offset + las::VLR_HEADER_SIZE); return las::EbVlr::create(*in_stream_, static_cast(vlrs[offset].data_length)); } return las::EbVlr(); diff --git a/cpp/src/io/copc_reader.cpp b/cpp/src/io/copc_reader.cpp index a316a343..a18e7b47 100644 --- a/cpp/src/io/copc_reader.cpp +++ b/cpp/src/io/copc_reader.cpp @@ -37,7 +37,7 @@ CopcInfo Reader::ReadCopcInfoVlr(std::map &vlrs) throw std::runtime_error("Reader::ReadCopcInfoVlr: COPC Info VLR was found in the wrong position, MUST be at " "offset 375 as per COPC specs."); - in_stream_->seekg(offset + lazperf::vlr_header::Size); + in_stream_->seekg(offset + las::VLR_HEADER_SIZE); return lazperf::copc_info_vlr::create(*in_stream_); } @@ -47,7 +47,7 @@ CopcExtents Reader::ReadCopcExtentsVlr(std::map &vlrs, auto extended_offset = FetchVlr(vlrs, "rock_robotic", 10001); if (offset != 0) { - in_stream_->seekg(offset + lazperf::vlr_header::Size); + in_stream_->seekg(offset + las::VLR_HEADER_SIZE); CopcExtents extents(las::CopcExtentsVlr::create(*in_stream_, static_cast(vlrs[offset].data_length)), static_cast(reader_->header().point_format_id), static_cast(eb_vlr.items.size()), extended_offset != 0); @@ -55,7 +55,7 @@ CopcExtents Reader::ReadCopcExtentsVlr(std::map &vlrs, // Load mean/var if they exist if (extended_offset != 0) { - in_stream_->seekg(extended_offset + lazperf::vlr_header::Size); + in_stream_->seekg(extended_offset + las::VLR_HEADER_SIZE); extents.SetExtendedStats( las::CopcExtentsVlr::create(*in_stream_, static_cast(vlrs[extended_offset].data_length))); } diff --git a/cpp/src/io/copc_writer_internal.cpp b/cpp/src/io/copc_writer_internal.cpp index 2f86112d..deab4e89 100644 --- a/cpp/src/io/copc_writer_internal.cpp +++ b/cpp/src/io/copc_writer_internal.cpp @@ -20,12 +20,12 @@ size_t WriterInternal::OffsetToPointData() const size_t base_laz_offset = laz::BaseWriter::OffsetToPointData(); // COPC VLR - size_t copc_info_vlr_size = (lazperf::vlr_header::Size + CopcInfo::VLR_SIZE_BYTES); + size_t copc_info_vlr_size = (las::VLR_HEADER_SIZE + CopcInfo::VLR_SIZE_BYTES); // COPC Extents VLR size_t copc_extents_vlr_size = CopcExtents::ByteSize(GetConfig()->LasHeader()->PointFormatId(), GetConfig()->ExtraBytesVlr().items.size()); - copc_extents_vlr_size += lazperf::vlr_header::Size; + copc_extents_vlr_size += las::VLR_HEADER_SIZE; // If we store extended stats we need two extents VLRs if (GetConfig()->CopcExtents()->HasExtendedStats()) copc_extents_vlr_size *= 2; diff --git a/cpp/src/io/laz_base_writer.cpp b/cpp/src/io/laz_base_writer.cpp index b6b3d6b6..dc72d9ed 100644 --- a/cpp/src/io/laz_base_writer.cpp +++ b/cpp/src/io/laz_base_writer.cpp @@ -13,13 +13,13 @@ size_t BaseWriter::OffsetToPointData() const // LAS Extra Byte VLR size_t las_eb_vlr_size = config_->ExtraBytesVlr().size(); if (las_eb_vlr_size > 0) - las_eb_vlr_size += lazperf::vlr_header::Size; + las_eb_vlr_size += las::VLR_HEADER_SIZE; // LAZ VLR size_t laz_vlr_size = lazperf::laz_vlr(config_->LasHeader().PointFormatId(), config_->LasHeader().EbByteSize(), VARIABLE_CHUNK_SIZE) .size(); - laz_vlr_size += lazperf::vlr_header::Size; + laz_vlr_size += las::VLR_HEADER_SIZE; return las::LasHeader::HEADER_SIZE_BYTES + las_eb_vlr_size + laz_vlr_size; } diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index a8778786..03489a7e 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -18,11 +18,11 @@ find_package(pybind11 CONFIG REQUIRED) # add the python lib pybind11_add_module(${COPC_PYTHON_LIB} bindings.cpp) -if (LAZPERF_IS_FOUND) - target_link_libraries(${COPC_PYTHON_LIB} PRIVATE lazperf_s copc-lib-s) -else() +if (LAZPERF_FOUND) target_link_libraries(${COPC_PYTHON_LIB} PRIVATE ${LAZPERF_LIB_NAME} copc-lib-s) -endif() +else () + target_link_libraries(${COPC_PYTHON_LIB} PRIVATE lazperf_s copc-lib-s) +endif () install(TARGETS ${COPC_PYTHON_LIB} DESTINATION .)