3MFConsortium / lib3mf

lib3mf is an implementation of the 3D Manufacturing Format file standard
http://3mf.io
BSD 2-Clause "Simplified" License
228 stars 92 forks source link

Release lib3mf as vcpkg #342

Closed martinweismann closed 1 month ago

martinweismann commented 7 months ago

Ideal outcome:

vijaiaeroastro commented 7 months ago

For starters, the lib3mf deployment needs to be made a valid CMake package.

# lib3mfConfig.cmake

# Initial setup for known components and default selection
set(lib3mf_known_components "C" "CDynamic" "Cpp" "CppDynamic")
set(lib3mf_selected_variant "CPPDynamic") # Default variant

# Check if any known component was specified and select it
foreach(comp ${lib3mf_FIND_COMPONENTS})
    if(comp IN_LIST lib3mf_known_components)
        set(lib3mf_selected_variant ${comp})
        break() # Use the first specified known component
    endif()
endforeach()

# Configure paths based on the selected variant
set(lib3mf_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../include/Bindings/${lib3mf_selected_variant}")
set(lib3mf_LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../lib")

# Adjust library file name based on platform
if(WIN32)
    set(lib3mf_LIBRARY "${lib3mf_LIBRARY_DIR}/lib3mf.dll")
    set(lib3mf_LIBRARY_IMPORT "${lib3mf_LIBRARY_DIR}/lib3mf.lib") # For importing symbols
elseif(APPLE)
    set(lib3mf_LIBRARY "${lib3mf_LIBRARY_DIR}/lib3mf.dylib")
else() # Linux and others
    set(lib3mf_LIBRARY "${lib3mf_LIBRARY_DIR}/lib3mf.so")
endif()

# Define the imported target
add_library(lib3mf::lib3mf SHARED IMPORTED)

# Set target properties for include directory and library path
set_target_properties(lib3mf::lib3mf PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES "${lib3mf_INCLUDE_DIR}"
    IMPORTED_LOCATION "${lib3mf_LIBRARY}"
)

if(WIN32)
    set_property(TARGET lib3mf PROPERTY IMPORTED_IMPLIB "${lib3mf_LIBRARY_IMPORT}")
endif()

This makes it possible to simply include lib3mf as follows

find_package(lib3mf CONFIG REQUIRED COMPONENTS CPPDynamic)

and linked simply as follows

target_link_libraries(hello3mf lib3mf::lib3mf)

It has been tested and works very well in CPP and CPPDynamic

I created this lib3mfConfig.cmake which actually makes it a valid CMake package. I have made preliminary tests with vcpkg too which works. This also works with Debian generator. So a .DEB and .RPM can also be generated for Ubuntu & RHEL style distros in Linux. This file needs to be included to cmake folder and some changes are required in the root CMakeLists.txt to also include this to the packaged headers and libraries.

The following changes make it possible to pack lib3mf with CPack. This will help in getting VCPKG, DEB, RPM and any other CMake compatible packages. One can simply generate a ZIP or tar ball and use it without having to reference .so or .dll files directly.

set(CPACK_PACKAGE_NAME "lib3mf")
set(CPACK_PACKAGE_VENDOR "3MF Consortium")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "lib3mf - An implementation of the 3D Manufacturing Format file standard")
set(CPACK_PACKAGE_VERSION "${LIB3MF_VERSION_MAJOR}.${LIB3MF_VERSION_MINOR}.${LIB3MF_VERSION_MICRO}")
set(CPACK_PACKAGE_VERSION_MAJOR "${LIB3MF_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${LIB3MF_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${LIB3MF_VERSION_MICRO}")
set(CPACK_PACKAGE_CONTACT "maintainer@maintainer.com")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "3mf Maintainer")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-Source")

include(CPack)

I will soon commit these changes in my branch once i am done with some additional tests.

martinweismann commented 7 months ago

Sounds good! Please make sure to use the develop-branch as base branch (and not master).