%global run_tests 0

%global bashcompletiondir %(pkg-config --variable=compatdir bash-completion)
%global cpuarch 64

%if 0%{?bootstrap}
%global with_mysql 0
%global mysql --without-mysql
%global with_poppler 0
%global poppler --without-poppler
%global with_spatialite 0
%global spatialite --without-spatialite
%else
# https://bugzilla.redhat.com/show_bug.cgi?id=1490492
%global with_mysql 1
%global mysql --with-mysql
# https://bugzilla.redhat.com/show_bug.cgi?id=1490492
%global with_poppler 1
%global poppler --with-poppler
%global with_spatialite 1
%global spatialite "--with-spatialite"
%endif

Name:          gdal
Version:       3.10.1
Release:       3
Summary:       GIS file format library
License:       MIT
URL:           http://www.gdal.org
Source0:       http://download.osgeo.org/gdal/%{version}/gdal-%{version}.tar.xz
Source1:       http://download.osgeo.org/%{name}/%{version}/%{name}autotest-%{version}.tar.gz
# Multilib compatible cpl-config.h header
Source2:       cpl-config.h
# Multilib compatible gdal-config script
Source3:       gdal-config

# Add some utils to the default install target
Patch0:        gdal_utils.patch
Patch1:        gdal_incompatible-pointer-types.patch

BuildRequires: cmake
BuildRequires: gcc-c++
BuildRequires: gmock-devel

BuildRequires: armadillo-devel
BuildRequires: bison
BuildRequires: cfitsio-devel
BuildRequires: CharLS-devel
BuildRequires: curl-devel
BuildRequires: expat-devel
BuildRequires: freexl-devel
BuildRequires: geos-devel
BuildRequires: giflib-devel
BuildRequires: gtest-devel
BuildRequires: hdf-devel
BuildRequires: hdf5-devel
BuildRequires: json-c-devel
BuildRequires: libdap-devel
BuildRequires: libgeotiff-devel
BuildRequires: libgta-devel
BuildRequires: libjpeg-devel
# BuildRequires: libkml-devel
# BuildRequires: liblerc-devel
BuildRequires: libpng-devel
BuildRequires: libpq-devel
%if %{with_spatialite}
BuildRequires: libspatialite-devel
%endif
BuildRequires: libtiff-devel
BuildRequires: libtirpc-devel
BuildRequires: libwebp-devel
BuildRequires: libzstd-devel
%if 0%{?with_mysql}
BuildRequires: mariadb-connector-c-devel
%endif
BuildRequires: netcdf-devel
BuildRequires: ogdi-devel
BuildRequires: openexr-devel
BuildRequires: openjpeg2-devel
BuildRequires: pcre2-devel
%if 0%{?with_poppler}
BuildRequires: poppler-devel
%endif
BuildRequires: proj-devel >= 5.2.0
BuildRequires: qhull-devel
BuildRequires: sqlite-devel
BuildRequires: swig
BuildRequires: unixODBC-devel
BuildRequires: xerces-c-devel
BuildRequires: xz-devel
BuildRequires: zlib-devel

# Python
BuildRequires: python3-devel
BuildRequires: python3-numpy
BuildRequires: python3-setuptools
BuildRequires: python3-pytest >= 3.6
BuildRequires: python3-lxml >= 4.5.1
BuildRequires: python3-filelock
BuildRequires: python3-pyproj

# Java
# For 'mvn_artifact' and 'mvn_install'
BuildRequires: ant
BuildRequires: java-devel >= 1:1.6.0
BuildRequires: javapackages-local
BuildRequires: jpackage-utils

# Run time dependency for gpsbabel driver
Requires:      gpsbabel
Requires:      %{name}-libs%{?_isa} = %{version}-%{release}


%description
Geospatial Data Abstraction Library (GDAL/OGR) is a cross platform
C++ translator library for raster and vector geospatial data formats.
As a library, it presents a single abstract data model to the calling
application for all supported formats. It also comes with a variety of
useful commandline utilities for data translation and processing.

It provides the primary data access engine for many applications.
GDAL/OGR is the most widely used geospatial data access library.


%package devel
Summary:       Development files for the GDAL file format library
Requires:      %{name}-libs%{?_isa} = %{version}-%{release}

%description devel
This package contains development files for GDAL.


%package libs
Summary:       GDAL file format library
# See frmts/grib/degrib/README.TXT
Provides:      bundled(g2lib) = 1.6.0
Provides:      bundled(degrib) = 2.14

%description libs
This package contains the GDAL file format library.

# No complete java yet in EL8
%package java
Summary:        Java modules for the GDAL file format library
Requires:       jpackage-utils
Requires:       %{name}-libs%{?_isa} = %{version}-%{release}

%description java
The GDAL Java modules provide support to handle multiple GIS file formats.


%package javadoc
Summary:        Javadocs for %{name}
Requires:       jpackage-utils
BuildArch:      noarch

%description javadoc
This package contains the API documentation for %{name}.


%package -n python3-gdal
%{?python_provide:%python_provide python3-gdal}
Summary:        Python modules for the GDAL file format library
Requires:       python3-numpy
Requires:       %{name}-libs%{?_isa} = %{version}-%{release}

%description -n python3-gdal
The GDAL Python 3 modules provide support to handle multiple GIS file formats.


%package python-tools
Summary:        Python tools for the GDAL file format library
Requires:       python3-gdal

%description python-tools
The GDAL Python package provides number of tools for programming and
manipulating GDAL file format library

%prep
%autosetup -N -p1 -n %{name}-%{version}

# Delete bundled libraries
rm -rf frmts/zlib
rm -rf frmts/png/libpng
rm -rf frmts/gif/giflib
rm -rf frmts/jpeg/libjpeg
rm -rf frmts/jpeg/libjpeg12
rm -rf frmts/gtiff/libgeotiff
rm -rf frmts/gtiff/libtiff
rm -rf mrf/LERCV1
rm -rf third_party/LercLib

# Setup autotest directory
tar xf %{SOURCE1}
mv %{name}autotest-%{version} autotest

# Need to patch autotest
# autotest/cpp uses private -O0 where it does not like any level of FORTIFY_SOURCE
sed -i -e 's/-D_FORTIFY_SOURCE=2/%{?_fortify_level_flags}%{!?_fortify_level_flags:-Wp,-D_FORTIFY_SOURCE=2}/g' autotest/cpp/CMakeLists.txt
sed -i "s/ \-O0)/)/g" ./autotest/cpp/CMakeLists.txt

%build
%cmake \
  -DCMAKE_INSTALL_INCLUDEDIR=include/gdal \
  -DGDAL_JAVA_INSTALL_DIR=%{_jnidir}/%{name} \
  -DGDAL_JAVA_JNI_INSTALL_DIR=%{_jnidir}/%{name} \
  -DGDAL_USE_JPEG12_INTERNAL=OFF \
  -DENABLE_DEFLATE64=OFF \
  -DGDAL_USE_LERC_INTERNAL=OFF
%cmake_build

%if %{with mingw}
%mingw_cmake \
  -DBUILD_TESTING=OFF \
  -DCMAKE_INSTALL_INCLUDEDIR=include/gdal \
  -DGDAL_USE_JPEG12_INTERNAL=OFF \
  -DENABLE_DEFLATE64=OFF \
  -DGDAL_USE_LERC_INTERNAL=OFF
%mingw_make_build
%endif


%install
%cmake_install

# List of manpages for python scripts
for file in %{buildroot}%{_bindir}/*.py; do
  if [ -f %{buildroot}%{_mandir}/man1/`basename ${file/.py/.1*}` ]; then
    echo "%{_mandir}/man1/`basename ${file/.py/.1*}`" >> gdal_python_manpages.txt
    echo "%exclude %{_mandir}/man1/`basename ${file/.py/.1*}`" >> gdal_python_manpages_excludes.txt
  fi
done

# Multilib
# - cpl_config.h is arch-dependent (contains various SIZEOF defines)
# - gdal-config stores arch-specific information
mv %{buildroot}%{_includedir}/%{name}/cpl_config.h %{buildroot}%{_includedir}/%{name}/cpl_config-%{cpuarch}.h
cp -a %{SOURCE2} %{buildroot}%{_includedir}/%{name}/cpl_config.h
mv %{buildroot}%{_bindir}/%{name}-config %{buildroot}%{_bindir}/%{name}-config-%{cpuarch}
cp -a %{SOURCE3} %{buildroot}%{_bindir}/%{name}-config


%if 0%{run_tests}
%check
%ctest || :
%endif


%files -f gdal_python_manpages_excludes.txt
%{_bindir}/gdal2tiles
%{_bindir}/gdal2xyz
%{_bindir}/gdal_calc
%{_bindir}/gdal_edit
%{_bindir}/gdal_fillnodata
%{_bindir}/gdal_footprint
%{_bindir}/gdal_merge
%{_bindir}/gdal_pansharpen
%{_bindir}/gdal_polygonize
%{_bindir}/gdal_proximity
%{_bindir}/gdal_retile
%{_bindir}/gdal_sieve
%{_bindir}/gdalattachpct
%{_bindir}/gdalcompare
%{_bindir}/gdalmove
%{_bindir}/ogr_layer_algebra
%{_bindir}/ogrmerge
%{_bindir}/pct2rgb
%{_bindir}/rgb2pct
%{_bindir}/gdal_contour
%{_bindir}/gdal_create
%{_bindir}/gdal_grid
%{_bindir}/gdal_rasterize
%{_bindir}/gdal_translate
%{_bindir}/gdal_viewshed
%{_bindir}/gdaladdo
%{_bindir}/gdalbuildvrt
%{_bindir}/gdaldem
%{_bindir}/gdalenhance
%{_bindir}/gdalinfo
%{_bindir}/gdallocationinfo
%{_bindir}/gdalmanage
%{_bindir}/gdalmdiminfo
%{_bindir}/gdalmdimtranslate
%{_bindir}/gdalsrsinfo
%{_bindir}/gdaltindex
%{_bindir}/gdaltransform
%{_bindir}/gdalwarp
%{_bindir}/gnmanalyse
%{_bindir}/gnmmanage
%{_bindir}/nearblack
%{_bindir}/ogr2ogr
%{_bindir}/ogrinfo
%{_bindir}/ogrlineref
%{_bindir}/ogrtindex
%{_bindir}/sozip
%{_datadir}/bash-completion/completions/*
%exclude %{_datadir}/bash-completion/completions/*.py
%{_mandir}/man1/*
%exclude %{_mandir}/man1/gdal-config.1*
# Python manpages excluded in -f gdal_python_manpages_excludes.txt

%files libs
%license LICENSE.TXT
%doc NEWS.md PROVENANCE.TXT COMMITTERS 
%{_libdir}/libgdal.so.36
%{_libdir}/libgdal.so.36.*
%{_datadir}/%{name}/
%{_libdir}/gdalplugins/

%files devel
%{_bindir}/%{name}-config
%{_bindir}/%{name}-config-%{cpuarch}
%{_includedir}/%{name}/
%{_libdir}/lib%{name}.so
%{_libdir}/cmake/gdal/
%{_libdir}/pkgconfig/%{name}.pc
%{_mandir}/man1/gdal-config.1*

%files -n python3-gdal
%doc swig/python/README.rst
%{python3_sitearch}/GDAL-%{version}-py*.egg-info/
%{python3_sitearch}/osgeo/
%{python3_sitearch}/osgeo_utils/

%files python-tools -f gdal_python_manpages.txt
%{_bindir}/gdal_calc.py
%{_bindir}/gdal_edit.py
%{_bindir}/gdal_fillnodata.py
%{_bindir}/gdal_merge.py
%{_bindir}/gdal_pansharpen.py
%{_bindir}/gdal_polygonize.py
%{_bindir}/gdal_proximity.py
%{_bindir}/gdal_retile.py
%{_bindir}/gdal_sieve.py
%{_bindir}/gdal2tiles.py
%{_bindir}/gdal2xyz.py
%{_bindir}/gdalattachpct.py
%{_bindir}/gdalcompare.py
%{_bindir}/gdalmove.py
%{_bindir}/ogr_layer_algebra.py
%{_bindir}/ogrmerge.py
%{_bindir}/pct2rgb.py
%{_bindir}/rgb2pct.py
%{_datadir}/bash-completion/completions/*.py

%files java
%{_jnidir}/%{name}/gdal-%{version}-sources.jar
%{_jnidir}/%{name}/gdal-%{version}.jar
%{_jnidir}/%{name}/gdal-%{version}.pom
%{_jnidir}/%{name}/libgdalalljni.so

%files javadoc
%{_jnidir}/%{name}/gdal-%{version}-javadoc.jar

%changelog
* Mon Mar 3 2025 laokz <zhangkai@iscas.ac.cn> - 3.10.1-3
- Add riscv64 to gdal-config source

* Thu Feb 27 2025 Dongxing Wang <dongxing.wang_a@thundersoft.com> - 3.10.1-2
- Fix build error with test job out of memory

* Tue Feb 11 2025 Dongxing Wang <dongxing.wang_a@thundersoft.com> - 3.10.1-1
- Update to 3.10.1

* Fri Nov 15 2024 Dongxing Wang <dongxing.wang_a@thundersoft.com> - 3.7.3-2
- Fix gdal-config-64 missing

* Tue Nov 5 2024 Dongxing Wang <dongxing.wang_a@thundersoft.com> - 3.7.3-1
- Update to 3.7.3
  CheckDependentLibraries.cmake: don't use libjpeg if disabled
  frmts/mrf/CMakeLists.txt: ignore HAVE_JPEGTURBO_DUAL_MODE_8_12 if GDAL_USE_JPEG is OFF
  FindPostgreSQL.cmake: Update the list of released PgSQL versions
  Add support for PoDoFo >= 0.10.0
  Add support for TileDB 2.17
  Fix build with -DRENAME_INTERNAL_SHAPELIB_SYMBOLS=OFF

* Thu Aug 29 2024 Dongxing Wang <dongxing.wang_a@thundersoft.com> - 3.7.2-1
- Update to 3.7.2

* Fri Apr 01 2022 tanyulong <tanyulong@kylinos.cn> - 2.3.1-2
- add config.rpath and remove jasper ,fix compile error

* Fri Oct 8 2021 zhaoshuang <zhaoshuang@uniontech.com> - 2.3.1-1
- Init package
