Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions scripts/ci/emscripten/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,66 @@ else
fi

# --- 6.5 Create C Wrappers ---
# Helper functions to extract some info from PROJ in C
# Helper functions to extract some info from PROJ in C and C++
log_step "6.5 Creating C Wrapper Functions"

WRAPPER_FILE="${TEMP_BUILD_DIR}/proj_wrappers.c"
WRAPPER_FILE="${TEMP_BUILD_DIR}/proj_wrappers.cpp"
WRAPPER_OBJ_FILE="${TEMP_BUILD_DIR}/proj_wrappers.o"

DDD=`date +"%Y-%m-%dT%H:%M:%S%z" -u`
cat << EOF > ${WRAPPER_FILE}
#include "proj.h"
#include "math.h"

#include "projapps_lib.h"

#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <vector>
#include <string>

void trampoline(PJ_PROJINFO_LOG_LEVEL level, const char *msg, void *user_data) {
if (!user_data) return;
const emscripten::val* js_callback = reinterpret_cast<const emscripten::val*>(user_data);
(*js_callback)(level, std::string(msg));
}

int projinfo_wrapper(uintptr_t ctx_ptr, emscripten::val jsArgs, emscripten::val jsCallback) {
std::vector<std::string> args = emscripten::vecFromJSArray<std::string>(jsArgs);
std::vector<char*> argv;
for (auto& s : args) {
argv.push_back(&s[0]);
}
PJ_CONTEXT *ctx = reinterpret_cast<PJ_CONTEXT *>(ctx_ptr);
return projinfo(ctx, args.size(), argv.data(), &trampoline, &jsCallback);
}

EMSCRIPTEN_BINDINGS(proj_module) {
emscripten::value_object<PJ_INFO>("PJ_INFO")
.field("major", &PJ_INFO::major)
.field("minor", &PJ_INFO::minor)
.field("patch", &PJ_INFO::patch)
.field("release", std::function<std::string(const PJ_INFO&)>([](const PJ_INFO& i) {
return std::string(i.release); }),
std::function<void(PJ_INFO&, std::string)>([](PJ_INFO& i, std::string v) {}))
.field("version", std::function<std::string(const PJ_INFO&)>([](const PJ_INFO& i) {
return std::string(i.version); }),
std::function<void(PJ_INFO&, std::string)>([](PJ_INFO& i, std::string v) {}));
emscripten::function("proj_info_ems", &proj_info);

emscripten::enum_<PJ_PROJINFO_LOG_LEVEL>("PJ_PROJINFO_LOG_LEVEL")
.value("INFO", PJ_PROJINFO_LOG_LEVEL_INFO)
.value("WARN", PJ_PROJINFO_LOG_LEVEL_WARN)
.value("ERR", PJ_PROJINFO_LOG_LEVEL_ERR);

emscripten::function("projinfo_ems", &projinfo_wrapper, emscripten::allow_raw_pointers());
}

extern "C" {

const char* get_compilation_date() {
return "$DDD" ;
}

int get_proj_info_sizeof() {
return sizeof(PJ_INFO);
}
EOF

Expand All @@ -290,6 +333,7 @@ FINAL_LIBS="${INSTALL_DIR}/lib/libproj.a \
emcc -v ${FINAL_LIBS} \
-o ${INSTALL_DIR}/projModule.js \
-O3 \
-lembind \
-s STACK_OVERFLOW_CHECK=1 \
-s STACK_SIZE=5MB \
-s NO_DISABLE_EXCEPTION_CATCHING \
Expand All @@ -303,7 +347,6 @@ emcc -v ${FINAL_LIBS} \
-s ALLOW_MEMORY_GROWTH=1 \
-s EXPORTED_RUNTIME_METHODS="[ccall, cwrap, FS, HEAPF64, stringToNewUTF8, UTF8ToString, getValue]" \
-s EXPORTED_FUNCTIONS="[
_get_proj_info_sizeof,
_get_compilation_date,
_proj_info,
_proj_context_errno_string,
Expand Down
1 change: 1 addition & 0 deletions scripts/reference_exported_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ proj_grid_cache_set_ttl
proj_grid_get_info_from_database
proj_grid_info
proj_identify
projinfo
proj_info
proj_init_info
proj_insert_object_session_create
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${PROJ_C_WARN_FLAGS}>")
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${PROJ_CXX_WARN_FLAGS}>")

# 0: include the lib files for the apps.
include(apps/lib_projapps.cmake)

# First configure proj library
include(lib_proj.cmake)
add_subdirectory(apps)
Expand Down
4 changes: 4 additions & 0 deletions src/apps/lib_projapps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(SRC_PROJAPPS_LIBS
apps/projinfo_lib.cpp
)
set(HEADERS_PROJAPPS_LIBS apps/projapps_lib.h)
68 changes: 68 additions & 0 deletions src/apps/projapps_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/******************************************************************************
*
* Project: PROJ
* Purpose: projinfo C API
* Author: Javier Jimenez Shaw
*
******************************************************************************
* Copyright (c) 2025 Javier Jimenez Shaw
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/

#if !defined(PROJAPPS_LIB_H)
#define PROJAPPS_LIB_H

#include "proj.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
* Level for the output given by projinfo in its callback.
*/
typedef enum {
PJ_PROJINFO_LOG_LEVEL_INFO = 1,
PJ_PROJINFO_LOG_LEVEL_WARN = 2,
PJ_PROJINFO_LOG_LEVEL_ERR = 3,
} PJ_PROJINFO_LOG_LEVEL;

typedef void (*projinfo_cb_t)(PJ_PROJINFO_LOG_LEVEL level, const char *msg,
void *user_data);

/*
* Internal C implementation of projinfo CLI application.
* See https://proj.org/apps/projinfo.html for more documentation.
* @param ctx context. It can be nullptr.
* @param argc number for parameters in argv.
* @param argv list of char* with the command parameters of projinfo.
* It does not contain the program name as first parameter.
* @param cb callback that to get the output of projinfo.
* It can be very fragmented, no necesarily by lines.
* @param user_data pointer for data passed to the callback.
*/
int PROJ_DLL projinfo(PJ_CONTEXT *ctx, int argc, char **argv, projinfo_cb_t cb,
void *user_data);

#ifdef __cplusplus
}
#endif

#endif
Loading
Loading