Skip to content

Commit

Permalink
Initial Hunterization
Browse files Browse the repository at this point in the history
Compile as library properly

Support not building as library
  • Loading branch information
rbsheth committed Jun 29, 2022
1 parent 280ab89 commit 16e8a9d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
97 changes: 97 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
cmake_minimum_required(VERSION 3.0)

project(cgltf LANGUAGES C CXX VERSION 1.7)

# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)

option(BUILD_AS_LIBRARY "Build library" FALSE)
option(BUILD_SHARED_LIBS "Build shared libraries" FALSE)
option(BUILD_TESTS "Build tests" FALSE)

if(MSVC AND BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

set(CGLTF_SRC_LIST cgltf.c)
set(CGLTF_HDR_LIST cgltf.h cgltf_write.h)
if(BUILD_AS_LIBRARY)
if(BUILD_SHARED_LIBS)
add_library(cgltf SHARED ${CGLTF_SRC_LIST} ${CGLTF_HDR_LIST})
else()
add_library(cgltf STATIC ${CGLTF_SRC_LIST} ${CGLTF_HDR_LIST})
endif()
else()
add_library(cgltf INTERFACE)
endif()
target_include_directories(cgltf INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
)

if(BUILD_TESTS)
add_subdirectory(test)
endif()

set(GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(VERSION_CONFIG "${GENERATED_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(PROJECT_CONFIG "${GENERATED_DIR}/${PROJECT_NAME}Config.cmake")

set(GENERATED_CGLTF_HEADER "${GENERATED_DIR}/cgltf.h")
set(GENERATED_CGLTF_WRITE_HEADER "${GENERATED_DIR}/cgltf_write.h")

if(BUILD_AS_LIBRARY)
target_compile_definitions(cgltf INTERFACE CGLTF_USE_HUNTER)

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/cgltf.h" CGLTF_HEADER_STRING)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/header.template.in" "${GENERATED_CGLTF_HEADER}")

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/cgltf_write.h" CGLTF_HEADER_STRING)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/header.template.in" "${GENERATED_CGLTF_WRITE_HEADER}")

set(CGLTF_HDR_LIST "${GENERATED_CGLTF_HEADER}" "${GENERATED_CGLTF_WRITE_HEADER}")
endif()

set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(NAMESPACE "${PROJECT_NAME}::")

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${VERSION_CONFIG}" COMPATIBILITY ExactVersion
)

configure_package_config_file(
"cmake/Config.cmake.in"
"${PROJECT_CONFIG}"
INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}"
)

install(
TARGETS cgltf
EXPORT "${TARGETS_EXPORT_NAME}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

install(
FILES ${CGLTF_HDR_LIST}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)

install(
FILES "${PROJECT_CONFIG}" "${VERSION_CONFIG}"
DESTINATION "${CONFIG_INSTALL_DIR}"
)

install(
EXPORT "${TARGETS_EXPORT_NAME}"
NAMESPACE "${NAMESPACE}"
DESTINATION "${CONFIG_INSTALL_DIR}"
)
3 changes: 3 additions & 0 deletions cgltf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define CGLTF_IMPLEMENTATION
#define CGLTF_WRITE_IMPLEMENTATION
#include "cgltf_write.h"
16 changes: 16 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2016, Ruslan Baratov
#
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://opensource.org/licenses/MIT
#
# 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.

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake")
check_required_components("@PROJECT_NAME@")
6 changes: 6 additions & 0 deletions cmake/header.template.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifdef CGLTF_USE_HUNTER
#undef CGLTF_IMPLEMENTATION
#undef CGLTF_WRITE_IMPLEMENTATION
#endif

@CGLTF_HEADER_STRING@

0 comments on commit 16e8a9d

Please sign in to comment.