Skip to content

Commit

Permalink
Also embed proj.ini if EMBED_RESOURCE_FILES=ON
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 9, 2024
1 parent 9fc42bb commit f26ff65
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
9 changes: 4 additions & 5 deletions data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if(NOT IS_DIRECTORY ${PROJ_DB_CACHE_DIR})
endif()
endif()

set(CONFIG_FILES
set(PROJ_INI
proj.ini
)

Expand Down Expand Up @@ -60,7 +60,7 @@ add_custom_command(
add_custom_target(generate_proj_db ALL DEPENDS ${PROJ_DB})

if(NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
foreach(FILE ${CONFIG_FILES} ${PROJ_DICTIONARY} ${GRIDSHIFT_FILES})
foreach(FILE ${PROJ_INI} ${PROJ_DICTIONARY} ${GRIDSHIFT_FILES})
configure_file(${FILE} ${FILE} COPYONLY)
endforeach()
endif()
Expand All @@ -78,7 +78,7 @@ set(DATA_FOR_TESTS
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/for_tests)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/for_tests/tests)

foreach(FILE ${DATA_FOR_TESTS} ${CONFIG_FILES})
foreach(FILE ${DATA_FOR_TESTS} ${PROJ_INI})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${CMAKE_CURRENT_BINARY_DIR}/for_tests/${FILE} COPYONLY)
endforeach()

Expand Down Expand Up @@ -109,15 +109,14 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/conus "${CMAKE_CURRENT_BINARY_D
#install
#
set(ALL_DATA_FILE
${CONFIG_FILES}
${PROJ_DICTIONARY}
${GRIDSHIFT_FILES}
${SCHEMA_FILES}
${GEOTIFF_FILES}
)

if (NOT USE_ONLY_EMBEDDED_RESOURCE_FILES)
list(APPEND ALL_DATA_FILE ${PROJ_DB})
list(APPEND ALL_DATA_FILE ${PROJ_INI} ${PROJ_DB})
endif()

install(
Expand Down
9 changes: 5 additions & 4 deletions docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,17 @@ All cached entries can be viewed using ``cmake -LAH`` from a build directory.

Default is OFF for shared library builds (BUILD_SHARED_LIBS=ON), and ON
for static library builds (BUILD_SHARED_LIBS=OFF).
When ON, :file:`proj.db` will be embedded into the PROJ library.
When ON, :file:`proj.db` and :file:`proj.ini` will be embedded into the PROJ library.

.. option:: USE_ONLY_EMBEDDED_RESOURCE_FILES=ON/OFF

.. versionadded:: 9.6

Even if EMBED_RESOURCE_FILES=ON, by default PROJ will still try to locate
:file:`proj.db` on the file system, and fallback to the embedded version if
not found. By setting USE_ONLY_EMBEDDED_RESOURCE_FILES=ON, no attempt
at locating :file:`proj.db` on the file system is made. Default is OFF.
:file:`proj.db` and :file:`proj.ini` on the file system, and fallback to the
embedded version if not found.
By setting USE_ONLY_EMBEDDED_RESOURCE_FILES=ON, no attempt at locating
those files on the file system is made. Default is OFF.
Users will also typically want to set EMBED_PROJ_DATA_PATH=OFF if setting
USE_ONLY_EMBEDDED_RESOURCE_FILES=OFF.

Expand Down
17 changes: 17 additions & 0 deletions src/embedded_resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@ const unsigned char *pj_get_embedded_proj_db(unsigned int *pnSize) {
*pnSize = (unsigned int)sizeof(proj_db);
return proj_db;
}

const char *pj_get_embedded_proj_ini(unsigned int *pnSize) {
static const char proj_ini[] = {
#embed PROJ_INI
};
*pnSize = (unsigned int)sizeof(proj_ini);
return proj_ini;
}

#else

#include "file_embed/proj_db.h"
const unsigned char *pj_get_embedded_proj_db(unsigned int *pnSize) {
*pnSize = proj_db_size;
return proj_db_data;
}

#include "file_embed/proj_ini.h"
const char *pj_get_embedded_proj_ini(unsigned int *pnSize) {
*pnSize = proj_ini_size;
return (const char *)proj_ini_data;
}

#endif
1 change: 1 addition & 0 deletions src/embedded_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
#endif

const unsigned char *pj_get_embedded_proj_db(unsigned int *pnSize);
const char *pj_get_embedded_proj_ini(unsigned int *pnSize);

#ifdef __cplusplus
}
Expand Down
41 changes: 28 additions & 13 deletions src/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
#include <unistd.h>
#endif

#ifdef EMBED_RESOURCE_FILES
#include "embedded_resources.h"
#endif

//! @cond Doxygen_Suppress

using namespace NS_PROJ::internal;
Expand Down Expand Up @@ -1850,21 +1854,32 @@ void pj_load_ini(PJ_CONTEXT *ctx) {
}

ctx->iniFileLoaded = true;
auto file = std::unique_ptr<NS_PROJ::File>(
reinterpret_cast<NS_PROJ::File *>(pj_open_lib_internal(
ctx, "proj.ini", "rb", pj_open_file_with_manager, nullptr, 0)));
if (!file)
return;
file->seek(0, SEEK_END);
const auto filesize = file->tell();
if (filesize == 0 || filesize > 100 * 1024U)
return;
file->seek(0, SEEK_SET);
std::string content;
content.resize(static_cast<size_t>(filesize));
const auto nread = file->read(&content[0], content.size());
if (nread != content.size())
std::unique_ptr<NS_PROJ::File> file;
#ifndef USE_ONLY_EMBEDDED_RESOURCE_FILES
file.reset(reinterpret_cast<NS_PROJ::File *>(pj_open_lib_internal(
ctx, "proj.ini", "rb", pj_open_file_with_manager, nullptr, 0)));
#endif
if (!file) {
#ifdef EMBED_RESOURCE_FILES
unsigned int content_size = 0;
const char *c_content = pj_get_embedded_proj_ini(&content_size);
content.assign(c_content, content_size);
#else
return;
#endif
}
if (file) {
file->seek(0, SEEK_END);
const auto filesize = file->tell();
if (filesize == 0 || filesize > 100 * 1024U)
return;
file->seek(0, SEEK_SET);
content.resize(static_cast<size_t>(filesize));
const auto nread = file->read(&content[0], content.size());
if (nread != content.size())
return;
}
content += '\n';
size_t pos = 0;
while (pos != std::string::npos) {
Expand Down
12 changes: 12 additions & 0 deletions src/lib_proj.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,21 @@ if (EMBED_RESOURCE_FILES AND NOT IS_SHARP_EMBED_AVAILABLE_RES)
DEPENDS generate_proj_db "${PROJECT_BINARY_DIR}/data/proj.db"
)
target_sources(proj PRIVATE embedded_resources.c "${EMBEDDED_PROJ_DB}")

set(EMBEDDED_PROJ_INI "file_embed/proj_ini.c")
add_custom_command(
OUTPUT "${EMBEDDED_PROJ_INI}"
COMMAND ${CMAKE_COMMAND}
-DRUN_FILE_EMBED_GENERATE=1
"-DFILE_EMBED_GENERATE_PATH=${PROJECT_SOURCE_DIR}/data/proj.ini"
-P ${PROJECT_SOURCE_DIR}/cmake/FileEmbed.cmake
DEPENDS "${PROJECT_SOURCE_DIR}/data/proj.ini"
)
target_sources(proj PRIVATE embedded_resources.c "${EMBEDDED_PROJ_DB}" "${EMBEDDED_PROJ_INI}")
elseif(EMBED_RESOURCE_FILES AND IS_SHARP_EMBED_AVAILABLE_RES)
add_library(proj_resources OBJECT embedded_resources.c)
target_compile_definitions(proj_resources PRIVATE "PROJ_DB=\"${PROJECT_BINARY_DIR}/data/proj.db\"")
target_compile_definitions(proj_resources PRIVATE "PROJ_INI=\"${PROJECT_SOURCE_DIR}/data/proj.ini\"")
target_compile_definitions(proj_resources PRIVATE USE_SHARP_EMBED)
add_dependencies(proj_resources generate_proj_db)
option(PROJ_OBJECT_LIBRARIES_POSITION_INDEPENDENT_CODE "Set ON to produce -fPIC code" ${BUILD_SHARED_LIBS})
Expand Down
1 change: 1 addition & 0 deletions test/cli/test_cs2cs_various.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ tests:
PROJ_DATA: $tmpdir
PROJ_DEBUG: 2
PROJ_SKIP_READ_USER_WRITABLE_DIRECTORY: YES
PROJ_ONLY_BEST_DEFAULT: ""
args: EPSG:4326+5773 EPSG:4326+5782
in: 39 -3 0
grep-v: pj_open_lib
Expand Down

0 comments on commit f26ff65

Please sign in to comment.