Documentation
Building gdal-java on Mac OS X

Building gdal on Mac OS X

The following procedure has been adapted form the GDAL CI (opens in a new tab).

set -e
 
xcode-select --installs
 
brew install cmake
brew install proj
brew install boost
brew install swig
brew install sqlite
brew install thrift
brew install --cask miniconda
 
git clone git@github.com:OSGeo/gdal.git
git checkout v3.6.4
 
conda update -n base -c defaults conda
conda install -y compilers automake pkgconfig cmake
conda config --set channel_priority strict
conda install --yes --quiet proj=7.1.1=h45baca5_3 python=3.8 swig -y
conda install --yes --quiet libgdal=3.1.4=hd7bf8dc_0  --only-deps -y
 
CONDA_PREFIX=/usr/local/miniconda/envs/test
find ${CONDA_PREFIX}/lib -name '*.la' -delete
 
mkdir build
cd build
 
cmake .. \
        -DCMAKE_INSTALL_PREFIX=$HOME/install-gdal \
        -DCMAKE_PREFIX_PATH=${CONDA_PREFIX} \
        -DGDAL_USE_GEOTIFF_INTERNAL=ON \
        -DGDAL_USE_PNG_INTERNAL=ON \
        -DGDAL_USE_POSTGRESQL=OFF \
        -DGDAL_USE_WEBP=OFF \
        -DGDAL_USE_ARROW=OFF \
        -DGDAL_USE_PARQUET=OFF \
        -DBUILD_CSHARP_BINDINGS=OFF
cmake --build .
cmake --build . --target install

After this, the library can be found in ~/install-gdal/share/java/libgdalalljni.dylib.

When starting a java application, the library can be loaded with the following command:

java -Djava.library.path=~/install-gdal/share/java -jar myapp.jar

Fix cmake file

diff --git a/cmake/modules/packages/FindSPATIALITE.cmake b/cmake/modules/packages/FindSPATIALITE.cmake
index 80052a4d48..6e5744187e 100644
--- a/cmake/modules/packages/FindSPATIALITE.cmake
+++ b/cmake/modules/packages/FindSPATIALITE.cmake
@@ -55,8 +55,8 @@ mark_as_advanced(SPATIALITE_LIBRARY SPATIALITE_INCLUDE_DIR)
 if(SPATIALITE_LIBRARY AND SPATIALITE_INCLUDE_DIR
    AND NOT SPATIALITE_VERSION_STRING)
     file(STRINGS "${SPATIALITE_INCLUDE_DIR}/spatialite.h" _spatialite_h_ver
-         REGEX "^[ \t]version[ \t]([0-9]+\\.[0-9]+),.*")
-    string(REGEX REPLACE "[ \t]version[ \t]([0-9]+\\.[0-9]+),.*" "\\1" _spatialite_h_ver ${_spatialite_h_ver})
+         REGEX "^[ \t]version[ \t]([0-9]+\\.[0-9]+\\.[0-9]+),.*")
+    string(REGEX REPLACE "[ \t]version[ \t]([0-9]+\\.[0-9]+\\.[0-9]+),.*" "\\1" _spatialite_h_ver ${_spatialite_h_ver})
     set(SPATIALITE_VERSION_STRING "${_spatialite_h_ver}")
 endif()

Sources

https://github.com/adoptium/temurin-build/issues/1671 (opens in a new tab)

https://stackoverflow.com/questions/52911791/hardened-runtime-for-java-and-mojave (opens in a new tab)

https://stackoverflow.com/questions/52905940/how-to-codesign-and-enable-the-hardened-runtime-for-a-3rd-party-cli-on-xcode (opens in a new tab)