From 1e9a4b3f2da67717b1a8f31938a6a5f78500f67e Mon Sep 17 00:00:00 2001 From: ev3nvy <12035264+ev3nvy@users.noreply.github.com> Date: Thu, 2 Oct 2025 20:36:12 +0200 Subject: [PATCH 1/4] build: add find_package support, improve add_subdirectory behavior --- CMakeLists.txt | 115 +++++++++++++----- cmake/SGP4Config.cmake.in | 8 ++ cmake/pkg-config.pc.in | 11 ++ {libsgp4 => include/SGP4}/CoordGeodetic.h | 0 {libsgp4 => include/SGP4}/CoordTopocentric.h | 0 {libsgp4 => include/SGP4}/DateTime.h | 0 {libsgp4 => include/SGP4}/DecayedException.h | 0 {libsgp4 => include/SGP4}/Eci.h | 0 {libsgp4 => include/SGP4}/Globals.h | 0 {libsgp4 => include/SGP4}/Observer.h | 0 {libsgp4 => include/SGP4}/OrbitalElements.h | 0 {libsgp4 => include/SGP4}/SGP4.h | 0 .../SGP4}/SatelliteException.h | 0 {libsgp4 => include/SGP4}/SolarPosition.h | 0 {libsgp4 => include/SGP4}/TimeSpan.h | 0 {libsgp4 => include/SGP4}/Tle.h | 0 {libsgp4 => include/SGP4}/TleException.h | 0 {libsgp4 => include/SGP4}/Util.h | 0 {libsgp4 => include/SGP4}/Vector.h | 0 libsgp4/CMakeLists.txt | 41 ------- libsgp4/DecayedException.cc | 1 - libsgp4/SatelliteException.cc | 1 - libsgp4/TleException.cc | 1 - src/CMakeLists.txt | 79 ++++++++++++ .../CoordGeodetic.cpp | 2 +- .../Globals.cc => src/CoordTopocentric.cpp | 2 +- libsgp4/DateTime.cc => src/DateTime.cpp | 2 +- src/DecayedException.cpp | 1 + libsgp4/Eci.cc => src/Eci.cpp | 6 +- libsgp4/Vector.cc => src/Globals.cpp | 2 +- libsgp4/Observer.cc => src/Observer.cpp | 4 +- .../OrbitalElements.cpp | 4 +- libsgp4/SGP4.cc => src/SGP4.cpp | 10 +- src/SatelliteException.cpp | 1 + .../SolarPosition.cc => src/SolarPosition.cpp | 6 +- libsgp4/CoordGeodetic.cc => src/TimeSpan.cpp | 2 +- libsgp4/Tle.cc => src/Tle.cpp | 2 +- src/TleException.cpp | 1 + libsgp4/Util.cc => src/Util.cpp | 2 +- libsgp4/TimeSpan.cc => src/Vector.cpp | 2 +- 40 files changed, 211 insertions(+), 95 deletions(-) create mode 100644 cmake/SGP4Config.cmake.in create mode 100644 cmake/pkg-config.pc.in rename {libsgp4 => include/SGP4}/CoordGeodetic.h (100%) rename {libsgp4 => include/SGP4}/CoordTopocentric.h (100%) rename {libsgp4 => include/SGP4}/DateTime.h (100%) rename {libsgp4 => include/SGP4}/DecayedException.h (100%) rename {libsgp4 => include/SGP4}/Eci.h (100%) rename {libsgp4 => include/SGP4}/Globals.h (100%) rename {libsgp4 => include/SGP4}/Observer.h (100%) rename {libsgp4 => include/SGP4}/OrbitalElements.h (100%) rename {libsgp4 => include/SGP4}/SGP4.h (100%) rename {libsgp4 => include/SGP4}/SatelliteException.h (100%) rename {libsgp4 => include/SGP4}/SolarPosition.h (100%) rename {libsgp4 => include/SGP4}/TimeSpan.h (100%) rename {libsgp4 => include/SGP4}/Tle.h (100%) rename {libsgp4 => include/SGP4}/TleException.h (100%) rename {libsgp4 => include/SGP4}/Util.h (100%) rename {libsgp4 => include/SGP4}/Vector.h (100%) delete mode 100644 libsgp4/CMakeLists.txt delete mode 100644 libsgp4/DecayedException.cc delete mode 100644 libsgp4/SatelliteException.cc delete mode 100644 libsgp4/TleException.cc create mode 100644 src/CMakeLists.txt rename libsgp4/CoordTopocentric.cc => src/CoordGeodetic.cpp (95%) rename libsgp4/Globals.cc => src/CoordTopocentric.cpp (94%) rename libsgp4/DateTime.cc => src/DateTime.cpp (99%) create mode 100644 src/DecayedException.cpp rename libsgp4/Eci.cc => src/Eci.cpp (97%) rename libsgp4/Vector.cc => src/Globals.cpp (95%) rename libsgp4/Observer.cc => src/Observer.cpp (97%) rename libsgp4/OrbitalElements.cc => src/OrbitalElements.cpp (97%) rename libsgp4/SGP4.cc => src/SGP4.cpp (99%) create mode 100644 src/SatelliteException.cpp rename libsgp4/SolarPosition.cc => src/SolarPosition.cpp (96%) rename libsgp4/CoordGeodetic.cc => src/TimeSpan.cpp (95%) rename libsgp4/Tle.cc => src/Tle.cpp (99%) create mode 100644 src/TleException.cpp rename libsgp4/Util.cc => src/Util.cpp (98%) rename libsgp4/TimeSpan.cc => src/Vector.cpp (96%) diff --git a/CMakeLists.txt b/CMakeLists.txt index df6bc3b..c59b82a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,39 +1,98 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.13.4) -PROJECT(SGP4) +cmake_minimum_required(VERSION 3.16...4.0) -if (POLICY CMP0054) - cmake_policy(SET CMP0054 NEW) +project(SGP4 + VERSION 1.1.0 + DESCRIPTION "Simplified perturbations models" + HOMEPAGE_URL https://github.com/dnwrnr/sgp4 + LANGUAGES CXX +) + +set(MAIN_PROJECT OFF) +# determine if SGP4 is built as a subproject (using add_subdirectory) +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MAIN_PROJECT ON) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + + option(BUILD_SHARED_LIBS "Build SGP4 as a shared library." ON) +endif() + +option(SGP4_BUILD_SHARED_LIBS "Build sgp4 target as SHARED instead of STATIC." ${BUILD_SHARED_LIBS}) +option(SGP4_BUILD_STATIC_LIBS "Build sgp4-static target along with sgp4." ${MAIN_PROJECT}) +option(SGP4_INSTALL "Install CMake targets during install step." ${MAIN_PROJECT}) +option(SGP4_ENABLE_PIC "Enable position-independent code (ignored for sgp4 target when SGP4_BUILD_SHARED_LIBS is enabled)." ON) + +if(SGP4_BUILD_STATIC_LIBS AND NOT SGP4_BUILD_SHARED_LIBS) + message(WARNING "SGP4_BUILD_STATIC_LIBS only makes sense if BUILD_SHARED_LIBS is set to ON.") + message(WARNING "You might be building and installing two identical static libraries.") + message(WARNING "NOTE: to override BUILD_SHARED_LIBS for SGP4 only, you may use SGP4_BUILD_SHARED_LIBS.") endif() -if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) - set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "..." FORCE) +if(NOT DEFINED SGP4_DEFAULT_TARGET_NAME) + # Allow overriding the target name when using FetchContent / add_subdirectory. + set(SGP4_DEFAULT_TARGET_NAME sgp4) +endif() +if(NOT DEFINED SGP4_STATIC_TARGET_NAME) + # Allow overriding the target name when using FetchContent / add_subdirectory. + set(SGP4_STATIC_TARGET_NAME sgp4-static) endif() -set(CMAKE_INSTALL_LIBDIR "/lib64") -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) +set(SGP4_NAMESPACE_NAME libsgp4) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +add_subdirectory(src) -if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-long-long") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcast-align") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-conversion") -endif(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) -set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" +) -include_directories(libsgp4) +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + @ONLY +) -add_subdirectory(libsgp4) -add_subdirectory(sattrack) -add_subdirectory(runtest) -add_subdirectory(passpredict) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + COMPATIBILITY AnyNewerVersion +) -file(COPY SGP4-VER.TLE DESTINATION ${PROJECT_BINARY_DIR}) +if(SGP4_INSTALL) + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" + ) + + install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" + ) + + install(TARGETS ${SGP4_DEFAULT_TARGET_NAME} + EXPORT "${PROJECT_NAME}Targets" + INCLUDES DESTINATION ${CMAKE_INSTALL_DATADIR} + # .so on UNIX + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + # .dll on Windows + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + # .lib on Windows + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) + + if(SGP4_BUILD_STATIC_LIBS) + install(TARGETS ${SGP4_STATIC_TARGET_NAME} + EXPORT "${PROJECT_NAME}Targets" + INCLUDES DESTINATION ${CMAKE_INSTALL_DATADIR} + # .lib on Windows, .a on UNIX + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) + endif() + + install(EXPORT "${PROJECT_NAME}Targets" NAMESPACE ${SGP4_NAMESPACE_NAME}:: DESTINATION "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}") + + install(DIRECTORY "include/" TYPE INCLUDE) +endif() diff --git a/cmake/SGP4Config.cmake.in b/cmake/SGP4Config.cmake.in new file mode 100644 index 0000000..f8ec3e9 --- /dev/null +++ b/cmake/SGP4Config.cmake.in @@ -0,0 +1,8 @@ +# The following variables are defined: +# - SGP4_IS_DEFAULT_TARGET_SHARED - whether sgp4 target is SHARED or not +# - SGP4_STATIC_LIBS_BUILT - whether we have built sgp4-static target + +set(SGP4_IS_DEFAULT_TARGET_SHARED @SGP4_BUILD_SHARED_LIBS@) +set(SGP4_STATIC_LIBS_BUILT @SGP4_BUILD_STATIC_LIBS@) + +include("${CMAKE_CURRENT_LIST_DIR}/SGP4Targets.cmake") diff --git a/cmake/pkg-config.pc.in b/cmake/pkg-config.pc.in new file mode 100644 index 0000000..67d0729 --- /dev/null +++ b/cmake/pkg-config.pc.in @@ -0,0 +1,11 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: @PROJECT_NAME@ +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Libs: -L${libdir} -l@SGP4_DEFAULT_TARGET_NAME@ +Cflags: -I${includedir} diff --git a/libsgp4/CoordGeodetic.h b/include/SGP4/CoordGeodetic.h similarity index 100% rename from libsgp4/CoordGeodetic.h rename to include/SGP4/CoordGeodetic.h diff --git a/libsgp4/CoordTopocentric.h b/include/SGP4/CoordTopocentric.h similarity index 100% rename from libsgp4/CoordTopocentric.h rename to include/SGP4/CoordTopocentric.h diff --git a/libsgp4/DateTime.h b/include/SGP4/DateTime.h similarity index 100% rename from libsgp4/DateTime.h rename to include/SGP4/DateTime.h diff --git a/libsgp4/DecayedException.h b/include/SGP4/DecayedException.h similarity index 100% rename from libsgp4/DecayedException.h rename to include/SGP4/DecayedException.h diff --git a/libsgp4/Eci.h b/include/SGP4/Eci.h similarity index 100% rename from libsgp4/Eci.h rename to include/SGP4/Eci.h diff --git a/libsgp4/Globals.h b/include/SGP4/Globals.h similarity index 100% rename from libsgp4/Globals.h rename to include/SGP4/Globals.h diff --git a/libsgp4/Observer.h b/include/SGP4/Observer.h similarity index 100% rename from libsgp4/Observer.h rename to include/SGP4/Observer.h diff --git a/libsgp4/OrbitalElements.h b/include/SGP4/OrbitalElements.h similarity index 100% rename from libsgp4/OrbitalElements.h rename to include/SGP4/OrbitalElements.h diff --git a/libsgp4/SGP4.h b/include/SGP4/SGP4.h similarity index 100% rename from libsgp4/SGP4.h rename to include/SGP4/SGP4.h diff --git a/libsgp4/SatelliteException.h b/include/SGP4/SatelliteException.h similarity index 100% rename from libsgp4/SatelliteException.h rename to include/SGP4/SatelliteException.h diff --git a/libsgp4/SolarPosition.h b/include/SGP4/SolarPosition.h similarity index 100% rename from libsgp4/SolarPosition.h rename to include/SGP4/SolarPosition.h diff --git a/libsgp4/TimeSpan.h b/include/SGP4/TimeSpan.h similarity index 100% rename from libsgp4/TimeSpan.h rename to include/SGP4/TimeSpan.h diff --git a/libsgp4/Tle.h b/include/SGP4/Tle.h similarity index 100% rename from libsgp4/Tle.h rename to include/SGP4/Tle.h diff --git a/libsgp4/TleException.h b/include/SGP4/TleException.h similarity index 100% rename from libsgp4/TleException.h rename to include/SGP4/TleException.h diff --git a/libsgp4/Util.h b/include/SGP4/Util.h similarity index 100% rename from libsgp4/Util.h rename to include/SGP4/Util.h diff --git a/libsgp4/Vector.h b/include/SGP4/Vector.h similarity index 100% rename from libsgp4/Vector.h rename to include/SGP4/Vector.h diff --git a/libsgp4/CMakeLists.txt b/libsgp4/CMakeLists.txt deleted file mode 100644 index f70d73b..0000000 --- a/libsgp4/CMakeLists.txt +++ /dev/null @@ -1,41 +0,0 @@ -set(SRCS - CoordGeodetic.cc - CoordTopocentric.cc - DateTime.cc - DecayedException.cc - Eci.cc - Globals.cc - Observer.cc - OrbitalElements.cc - SGP4.cc - SatelliteException.cc - SolarPosition.cc - TimeSpan.cc - Tle.cc - TleException.cc - Util.cc - Vector.cc) - - set(INCS - CoordGeodetic.h - CoordTopocentric.h - DateTime.h - DecayedException.h - Eci.h - Globals.h - Observer.h - OrbitalElements.h - SatelliteException.h - SGP4.h - SolarPosition.h - TimeSpan.h - TleException.h - Tle.h - Util.h - Vector.h - ) - -add_library(sgp4 STATIC ${SRCS} ${INCS}) -add_library(sgp4s SHARED ${SRCS} ${INCS}) -install( TARGETS sgp4s LIBRARY DESTINATION lib ) -install( FILES ${INCS} DESTINATION include/libsgp4 ) diff --git a/libsgp4/DecayedException.cc b/libsgp4/DecayedException.cc deleted file mode 100644 index 2b4c522..0000000 --- a/libsgp4/DecayedException.cc +++ /dev/null @@ -1 +0,0 @@ -#include "DecayedException.h" diff --git a/libsgp4/SatelliteException.cc b/libsgp4/SatelliteException.cc deleted file mode 100644 index 5fe1212..0000000 --- a/libsgp4/SatelliteException.cc +++ /dev/null @@ -1 +0,0 @@ -#include "SatelliteException.h" diff --git a/libsgp4/TleException.cc b/libsgp4/TleException.cc deleted file mode 100644 index 6a23d01..0000000 --- a/libsgp4/TleException.cc +++ /dev/null @@ -1 +0,0 @@ -#include "TleException.h" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..6f43bea --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,79 @@ +set(HEADER_LIST + "${PROJECT_SOURCE_DIR}/include/SGP4/CoordGeodetic.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/CoordTopocentric.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/DateTime.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/DecayedException.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Eci.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Globals.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Observer.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/OrbitalElements.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/SGP4.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/SatelliteException.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/SolarPosition.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/TimeSpan.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Tle.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/TleException.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Util.h" + "${PROJECT_SOURCE_DIR}/include/SGP4/Vector.h" +) +set(SOURCE_LIST + CoordGeodetic.cpp + CoordTopocentric.cpp + DateTime.cpp + DecayedException.cpp + Eci.cpp + Globals.cpp + Observer.cpp + OrbitalElements.cpp + SGP4.cpp + SatelliteException.cpp + SolarPosition.cpp + TimeSpan.cpp + Tle.cpp + TleException.cpp + Util.cpp + Vector.cpp +) + +if(SGP4_BUILD_SHARED_LIBS) + add_library(${SGP4_DEFAULT_TARGET_NAME} SHARED ${SOURCE_LIST}) +else() + add_library(${SGP4_DEFAULT_TARGET_NAME} STATIC ${SOURCE_LIST}) + set_target_properties(${SGP4_DEFAULT_TARGET_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ${SGP4_ENABLE_PIC}) +endif() +set(SGP4_TARGETS ${SGP4_DEFAULT_TARGET_NAME}) + +add_library(${SGP4_NAMESPACE_NAME}::${SGP4_DEFAULT_TARGET_NAME} ALIAS ${SGP4_DEFAULT_TARGET_NAME}) + +if(SGP4_BUILD_STATIC_LIBS) + add_library(${SGP4_STATIC_TARGET_NAME} STATIC ${SOURCE_LIST}) + list(APPEND SGP4_TARGETS ${SGP4_STATIC_TARGET_NAME}) + + add_library(${SGP4_NAMESPACE_NAME}::${SGP4_STATIC_TARGET_NAME} ALIAS ${SGP4_STATIC_TARGET_NAME}) + set_target_properties(${SGP4_STATIC_TARGET_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ${SGP4_ENABLE_PIC}) +endif() + +foreach(TARGET ${SGP4_TARGETS}) + set_target_properties(${TARGET} PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}" + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF + # TODO: possibly replace with __declspec(dllexport) function annotations + WINDOWS_EXPORT_ALL_SYMBOLS ON + ) + + target_include_directories(${TARGET} PUBLIC $ $) + target_compile_features(${TARGET} PUBLIC cxx_std_17) + + target_compile_options(${TARGET} PRIVATE + $<$:/W4> + $<$>:-Wall;-Wextra;-Wshadow;-Wnon-virtual-dtor;-pedantic;-Wno-long-long;-Wcast-align;-Wsign-conversion> + ) +endforeach() + +source_group( + TREE "${PROJECT_SOURCE_DIR}/include" + PREFIX "Header Files" + FILES ${HEADER_LIST} +) diff --git a/libsgp4/CoordTopocentric.cc b/src/CoordGeodetic.cpp similarity index 95% rename from libsgp4/CoordTopocentric.cc rename to src/CoordGeodetic.cpp index a418a90..e711cfd 100644 --- a/libsgp4/CoordTopocentric.cc +++ b/src/CoordGeodetic.cpp @@ -15,4 +15,4 @@ */ -#include "CoordTopocentric.h" +#include diff --git a/libsgp4/Globals.cc b/src/CoordTopocentric.cpp similarity index 94% rename from libsgp4/Globals.cc rename to src/CoordTopocentric.cpp index dad80d5..6749ad8 100644 --- a/libsgp4/Globals.cc +++ b/src/CoordTopocentric.cpp @@ -15,4 +15,4 @@ */ -#include "Globals.h" +#include diff --git a/libsgp4/DateTime.cc b/src/DateTime.cpp similarity index 99% rename from libsgp4/DateTime.cc rename to src/DateTime.cpp index e056208..3642425 100644 --- a/libsgp4/DateTime.cc +++ b/src/DateTime.cpp @@ -15,7 +15,7 @@ */ -#include "DateTime.h" +#include #if 0 diff --git a/src/DecayedException.cpp b/src/DecayedException.cpp new file mode 100644 index 0000000..5989aab --- /dev/null +++ b/src/DecayedException.cpp @@ -0,0 +1 @@ +#include diff --git a/libsgp4/Eci.cc b/src/Eci.cpp similarity index 97% rename from libsgp4/Eci.cc rename to src/Eci.cpp index 61d24bc..611672b 100644 --- a/libsgp4/Eci.cc +++ b/src/Eci.cpp @@ -15,10 +15,10 @@ */ -#include "Eci.h" +#include -#include "Globals.h" -#include "Util.h" +#include +#include namespace libsgp4 { diff --git a/libsgp4/Vector.cc b/src/Globals.cpp similarity index 95% rename from libsgp4/Vector.cc rename to src/Globals.cpp index 1b12c95..9b02b5a 100644 --- a/libsgp4/Vector.cc +++ b/src/Globals.cpp @@ -15,4 +15,4 @@ */ -#include "Vector.h" +#include diff --git a/libsgp4/Observer.cc b/src/Observer.cpp similarity index 97% rename from libsgp4/Observer.cc rename to src/Observer.cpp index 2065eef..39408c1 100644 --- a/libsgp4/Observer.cc +++ b/src/Observer.cpp @@ -15,8 +15,8 @@ */ -#include "Observer.h" -#include "CoordTopocentric.h" +#include +#include namespace libsgp4 { diff --git a/libsgp4/OrbitalElements.cc b/src/OrbitalElements.cpp similarity index 97% rename from libsgp4/OrbitalElements.cc rename to src/OrbitalElements.cpp index 7e85720..2f1fd13 100644 --- a/libsgp4/OrbitalElements.cc +++ b/src/OrbitalElements.cpp @@ -15,9 +15,9 @@ */ -#include "OrbitalElements.h" +#include -#include "Tle.h" +#include namespace libsgp4 { diff --git a/libsgp4/SGP4.cc b/src/SGP4.cpp similarity index 99% rename from libsgp4/SGP4.cc rename to src/SGP4.cpp index ecd9026..7d84c97 100644 --- a/libsgp4/SGP4.cc +++ b/src/SGP4.cpp @@ -15,12 +15,12 @@ */ -#include "SGP4.h" +#include -#include "Util.h" -#include "Vector.h" -#include "SatelliteException.h" -#include "DecayedException.h" +#include +#include +#include +#include #include #include diff --git a/src/SatelliteException.cpp b/src/SatelliteException.cpp new file mode 100644 index 0000000..f44fbdc --- /dev/null +++ b/src/SatelliteException.cpp @@ -0,0 +1 @@ +#include diff --git a/libsgp4/SolarPosition.cc b/src/SolarPosition.cpp similarity index 96% rename from libsgp4/SolarPosition.cc rename to src/SolarPosition.cpp index cb6bbeb..1556eef 100644 --- a/libsgp4/SolarPosition.cc +++ b/src/SolarPosition.cpp @@ -15,10 +15,10 @@ */ -#include "SolarPosition.h" +#include -#include "Globals.h" -#include "Util.h" +#include +#include #include diff --git a/libsgp4/CoordGeodetic.cc b/src/TimeSpan.cpp similarity index 95% rename from libsgp4/CoordGeodetic.cc rename to src/TimeSpan.cpp index 0a92103..0f27d4f 100644 --- a/libsgp4/CoordGeodetic.cc +++ b/src/TimeSpan.cpp @@ -15,4 +15,4 @@ */ -#include "CoordGeodetic.h" +#include diff --git a/libsgp4/Tle.cc b/src/Tle.cpp similarity index 99% rename from libsgp4/Tle.cc rename to src/Tle.cpp index c74aaa3..18d90b9 100644 --- a/libsgp4/Tle.cc +++ b/src/Tle.cpp @@ -15,7 +15,7 @@ */ -#include "Tle.h" +#include #include diff --git a/src/TleException.cpp b/src/TleException.cpp new file mode 100644 index 0000000..b01d5e4 --- /dev/null +++ b/src/TleException.cpp @@ -0,0 +1 @@ +#include diff --git a/libsgp4/Util.cc b/src/Util.cpp similarity index 98% rename from libsgp4/Util.cc rename to src/Util.cpp index 3437aab..3983bde 100644 --- a/libsgp4/Util.cc +++ b/src/Util.cpp @@ -15,7 +15,7 @@ */ -#include "Util.h" +#include #include #include diff --git a/libsgp4/TimeSpan.cc b/src/Vector.cpp similarity index 96% rename from libsgp4/TimeSpan.cc rename to src/Vector.cpp index c426258..69aa53e 100644 --- a/libsgp4/TimeSpan.cc +++ b/src/Vector.cpp @@ -15,4 +15,4 @@ */ -#include "TimeSpan.h" +#include From d66d3ee770453b4b346caaf55f2f422c3227be93 Mon Sep 17 00:00:00 2001 From: ev3nvy <12035264+ev3nvy@users.noreply.github.com> Date: Fri, 3 Oct 2025 01:34:34 +0200 Subject: [PATCH 2/4] build,test: re-add tests, fix seemingly broken test --- CMakeLists.txt | 8 ++++++++ runtest/CMakeLists.txt | 7 ------- tests/CMakeLists.txt | 23 +++++++++++++++++++++++ SGP4-VER.TLE => tests/SGP4-VER.TLE | 0 runtest/runtest.cc => tests/runtest.cpp | 24 +++++++++++++----------- 5 files changed, 44 insertions(+), 18 deletions(-) delete mode 100644 runtest/CMakeLists.txt create mode 100644 tests/CMakeLists.txt rename SGP4-VER.TLE => tests/SGP4-VER.TLE (100%) rename runtest/runtest.cc => tests/runtest.cpp (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c59b82a..87c959e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,14 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) option(BUILD_SHARED_LIBS "Build SGP4 as a shared library." ON) + + include(CTest) endif() option(SGP4_BUILD_SHARED_LIBS "Build sgp4 target as SHARED instead of STATIC." ${BUILD_SHARED_LIBS}) option(SGP4_BUILD_STATIC_LIBS "Build sgp4-static target along with sgp4." ${MAIN_PROJECT}) option(SGP4_INSTALL "Install CMake targets during install step." ${MAIN_PROJECT}) +option(SGP4_BUILD_TESTS "Enable SGP4 tests." ${MAIN_PROJECT}) option(SGP4_ENABLE_PIC "Enable position-independent code (ignored for sgp4 target when SGP4_BUILD_SHARED_LIBS is enabled)." ON) if(SGP4_BUILD_STATIC_LIBS AND NOT SGP4_BUILD_SHARED_LIBS) @@ -40,6 +43,11 @@ set(SGP4_NAMESPACE_NAME libsgp4) add_subdirectory(src) +if(SGP4_BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() + include(GNUInstallDirs) include(CMakePackageConfigHelpers) diff --git a/runtest/CMakeLists.txt b/runtest/CMakeLists.txt deleted file mode 100644 index 621d56c..0000000 --- a/runtest/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -set(SRCS - runtest.cc) - -add_executable(runtest - ${SRCS}) -target_link_libraries(runtest - sgp4) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..63138d1 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,23 @@ +set(SOURCE_LIST runtest.cpp) + +add_executable(runtest ${SOURCE_LIST}) + +set_target_properties(runtest PROPERTIES + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF +) + +target_compile_features(runtest PRIVATE cxx_std_17) +target_compile_options(runtest PRIVATE + $<$:-Wall;-Wextra;-Wshadow;-Wnon-virtual-dtor;-pedantic;-Wno-long-long;-Wcast-align;-Wsign-conversion> +) +target_link_libraries(runtest PRIVATE libsgp4::sgp4) + +add_test(NAME runtest COMMAND runtest) + +if(WIN32 AND SGP4_BUILD_SHARED_LIBS) + # copy sgp4 dll into the tests directory + add_custom_command(TARGET runtest POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different "$" "$") +endif() +file(COPY SGP4-VER.TLE DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/SGP4-VER.TLE b/tests/SGP4-VER.TLE similarity index 100% rename from SGP4-VER.TLE rename to tests/SGP4-VER.TLE diff --git a/runtest/runtest.cc b/tests/runtest.cpp similarity index 95% rename from runtest/runtest.cc rename to tests/runtest.cpp index c3676a8..54befec 100644 --- a/runtest/runtest.cc +++ b/tests/runtest.cpp @@ -15,11 +15,11 @@ */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -159,7 +159,7 @@ void tokenize(const std::string& str, std::vector& tokens) } } -void RunTest(const char* infile) +int RunTest(const char* infile) { std::ifstream file; @@ -168,7 +168,7 @@ void RunTest(const char* infile) if (!file.is_open()) { std::cerr << "Error opening file" << std::endl; - return; + return 1; } bool got_first_line = false; @@ -176,6 +176,8 @@ void RunTest(const char* infile) std::string line2; std::string parameters; + auto error = 0; + while (!file.eof()) { std::string line; @@ -213,6 +215,7 @@ void RunTest(const char* infile) { std::cerr << "Error: " << e.what() << std::endl; std::cerr << line << std::endl; + error = 1; } } else @@ -259,6 +262,7 @@ void RunTest(const char* infile) { std::cerr << "Error: " << e.what() << std::endl; std::cerr << line << std::endl; + error = 1; } } } @@ -268,14 +272,12 @@ void RunTest(const char* infile) */ file.close(); - return; + return error; } int main() { const char* file_name = "SGP4-VER.TLE"; - RunTest(file_name); - - return 1; + return RunTest(file_name); } From a792954b2346bc56ed6290305e4177aa7e778a68 Mon Sep 17 00:00:00 2001 From: ev3nvy <12035264+ev3nvy@users.noreply.github.com> Date: Fri, 3 Oct 2025 01:36:40 +0200 Subject: [PATCH 3/4] build: re-add remaining executables as "examples" --- CMakeLists.txt | 5 +++++ examples/CMakeLists.txt | 2 ++ examples/passpredict/CMakeLists.txt | 20 +++++++++++++++++++ .../passpredict/passpredict.cpp | 10 +++++----- examples/sattrack/CMakeLists.txt | 20 +++++++++++++++++++ .../sattrack/sattrack.cpp | 8 ++++---- passpredict/CMakeLists.txt | 7 ------- sattrack/CMakeLists.txt | 7 ------- 8 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/passpredict/CMakeLists.txt rename passpredict/passpredict.cc => examples/passpredict/passpredict.cpp (98%) create mode 100644 examples/sattrack/CMakeLists.txt rename sattrack/sattrack.cc => examples/sattrack/sattrack.cpp (93%) delete mode 100644 passpredict/CMakeLists.txt delete mode 100644 sattrack/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 87c959e..d90aef7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ option(SGP4_BUILD_SHARED_LIBS "Build sgp4 target as SHARED instead of STATIC." option(SGP4_BUILD_STATIC_LIBS "Build sgp4-static target along with sgp4." ${MAIN_PROJECT}) option(SGP4_INSTALL "Install CMake targets during install step." ${MAIN_PROJECT}) option(SGP4_BUILD_TESTS "Enable SGP4 tests." ${MAIN_PROJECT}) +option(SGP4_BUILD_EXAMPLES "Enable SGP4 examples." ${MAIN_PROJECT}) option(SGP4_ENABLE_PIC "Enable position-independent code (ignored for sgp4 target when SGP4_BUILD_SHARED_LIBS is enabled)." ON) if(SGP4_BUILD_STATIC_LIBS AND NOT SGP4_BUILD_SHARED_LIBS) @@ -48,6 +49,10 @@ if(SGP4_BUILD_TESTS) add_subdirectory(tests) endif() +if(SGP4_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + include(GNUInstallDirs) include(CMakePackageConfigHelpers) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..72e915b --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(passpredict) +add_subdirectory(sattrack) diff --git a/examples/passpredict/CMakeLists.txt b/examples/passpredict/CMakeLists.txt new file mode 100644 index 0000000..850dd80 --- /dev/null +++ b/examples/passpredict/CMakeLists.txt @@ -0,0 +1,20 @@ +set(SOURCE_LIST passpredict.cpp) + +add_executable(passpredict ${SOURCE_LIST}) + +set_target_properties(passpredict PROPERTIES + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF +) + +target_compile_features(passpredict PRIVATE cxx_std_17) +target_compile_options(passpredict PRIVATE + $<$:-Wall;-Wextra;-Wshadow;-Wnon-virtual-dtor;-pedantic;-Wno-long-long;-Wcast-align;-Wsign-conversion> +) +target_link_libraries(passpredict PRIVATE libsgp4::sgp4) + +if(WIN32 AND SGP4_BUILD_SHARED_LIBS) + # copy sgp4 dll into the examples/passpredict directory + add_custom_command(TARGET passpredict POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different "$" "$") +endif() diff --git a/passpredict/passpredict.cc b/examples/passpredict/passpredict.cpp similarity index 98% rename from passpredict/passpredict.cc rename to examples/passpredict/passpredict.cpp index db6131e..c7e8cb3 100644 --- a/passpredict/passpredict.cc +++ b/examples/passpredict/passpredict.cpp @@ -15,11 +15,11 @@ */ -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include diff --git a/examples/sattrack/CMakeLists.txt b/examples/sattrack/CMakeLists.txt new file mode 100644 index 0000000..a6d88b8 --- /dev/null +++ b/examples/sattrack/CMakeLists.txt @@ -0,0 +1,20 @@ +set(SOURCE_LIST sattrack.cpp) + +add_executable(sattrack ${SOURCE_LIST}) + +set_target_properties(sattrack PROPERTIES + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF +) + +target_compile_features(sattrack PRIVATE cxx_std_17) +target_compile_options(sattrack PRIVATE + $<$:-Wall;-Wextra;-Wshadow;-Wnon-virtual-dtor;-pedantic;-Wno-long-long;-Wcast-align;-Wsign-conversion> +) +target_link_libraries(sattrack PRIVATE libsgp4::sgp4) + +if(WIN32 AND SGP4_BUILD_SHARED_LIBS) + # copy sgp4 dll into the examples/sattrack directory + add_custom_command(TARGET sattrack POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different "$" "$") +endif() diff --git a/sattrack/sattrack.cc b/examples/sattrack/sattrack.cpp similarity index 93% rename from sattrack/sattrack.cc rename to examples/sattrack/sattrack.cpp index 60c3548..f397e68 100644 --- a/sattrack/sattrack.cc +++ b/examples/sattrack/sattrack.cpp @@ -15,10 +15,10 @@ */ -#include -#include -#include -#include +#include +#include +#include +#include #include diff --git a/passpredict/CMakeLists.txt b/passpredict/CMakeLists.txt deleted file mode 100644 index 9975e9d..0000000 --- a/passpredict/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -set(SRCS - passpredict.cc) - -add_executable(passpredict - ${SRCS}) -target_link_libraries(passpredict - sgp4) diff --git a/sattrack/CMakeLists.txt b/sattrack/CMakeLists.txt deleted file mode 100644 index a8c5027..0000000 --- a/sattrack/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -set(SRCS - sattrack.cc) - -add_executable(sattrack - ${SRCS}) -target_link_libraries(sattrack - sgp4) From f1234b4ea89c6017e796022d7932b2b4ec1636a1 Mon Sep 17 00:00:00 2001 From: ev3nvy <12035264+ev3nvy@users.noreply.github.com> Date: Tue, 14 Oct 2025 20:41:15 +0200 Subject: [PATCH 4/4] docs: add `CMake` integration examples NOTE: none of these have been tested, they seem sane enough --- README.md | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dcd0498..6c812f2 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,92 @@ -SGP4 library -============ +# SGP4 library +## CMake support +### External +```cmake +# ./CMakeLists.txt -License -------- +find_package(SGP4 1.1 CONFIG REQUIRED) +# ... +add_library(foo ...) +# ... +target_link_libraries(foo PRIVATE libsgp4::sgp4) +# target_link_libraries(foo PRIVATE libsgp4::sgp4-static) # or static +``` + +### Embedded +```cmake +# ./CMakeLists.txt + +# this affects if the libsgp4::sgp4 target is shared or static +set(SGP4_BUILD_SHARED_LIBS ON) +# this affects if the libsgp4::sgp4-static target is available +set(SGP4_BUILD_STATIC_LIBS ON) + +add_subdirectory(thirdparty/sgp4) +# ... +add_library(foo ...) +# ... +target_link_libraries(foo PRIVATE libsgp4::sgp4) +# target_link_libraries(foo PRIVATE libsgp4::sgp4-static) # or static +``` + +### Embedded (FetchContent) +```cmake +# ./CMakeLists.txt + +include(FetchContent) + +# this affects if the libsgp4::sgp4 target is shared or static +set(SGP4_BUILD_SHARED_LIBS ON) +# this affects if the libsgp4::sgp4-static target is available +set(SGP4_BUILD_STATIC_LIBS ON) + +FetchContent_Declare(sgp4 + GIT_REPOSITORY https://github.com/dnwrnr/sgp4 + # TODO: update this with an actual commit + GIT_TAG bdff20d3eb6f010af95574990a662f8089db93f8 # https://github.com/dnwrnr/sgp4/tree/bdff20d3eb6f010af95574990a662f8089db93f8 +) +FetchContent_MakeAvailable(sgp4) +# ... +add_library(foo ...) +# ... +target_link_libraries(foo PRIVATE libsgp4::sgp4) +# target_link_libraries(foo PRIVATE libsgp4::sgp4-static) # or static +``` + +### External with embedded fallback +```cmake +# ./CMakeLists.txt + +# this affects if the libsgp4::sgp4 fallback target is shared or static +set(SGP4_BUILD_SHARED_LIBS ON) +# this affects if the libsgp4::sgp4-static fallback target is available +set(SGP4_BUILD_STATIC_LIBS ON) + +# use include instead of add_subdirectory, otherwise targets won't get propagated +include(thirdparty/CMakeLists.txt) +add_subdirectory(src) +``` + +```cmake +# ./src/CMakeLists.txt + +add_library(foo ...) +# ... +target_link_libraries(foo PRIVATE libsgp4::sgp4) +# target_link_libraries(foo PRIVATE libsgp4::sgp4-static) # or static +``` + +```cmake +# ./thirdparty/CMakeLists.txt + +find_package(SGP4 1.1 CONFIG) +if(NOT SGP4_FOUND) + message(STATUS "Using SGP4 submodule") + add_subdirectory("${CMAKE_SOURCE_DIR}/thirdparty/sgp4") +endif() +``` + +## License Copyright 2017 Daniel Warner