1111import os
1212import stat
1313import urllib .request
14+ from urllib .parse import urlparse
1415import shutil
1516import zipfile
17+ import tarfile
1618
1719from venv import EnvBuilder
1820
2628 help = 'Build VCPKG environment from scratch instead of downloading prebuilt one.' )
2729parser .add_argument ('--vcpkg-archive-url' ,
2830 type = str ,
29- default = 'https://github.com/OpenDroneMap/windows-deps/releases/download/2.6 .0/vcpkg-export.zip' ,
31+ default = 'https://github.com/OpenDroneMap/windows-deps/releases/download/2.7 .0/vcpkg-export.zip' ,
3032 required = False ,
3133 help = 'Path to VCPKG export archive' )
3234parser .add_argument ('--signtool-path' ,
4749
4850args = parser .parse_args ()
4951
50- def run (cmd , cwd = os .getcwd ()):
51- env = os .environ .copy ()
52+ def run (cmd , cwd = os .getcwd (), env = os .environ .copy ()):
5253 print (cmd )
5354 p = subprocess .Popen (cmd , shell = True , env = env , cwd = cwd )
5455 retcode = p .wait ()
@@ -71,15 +72,42 @@ def vcpkg_requirements():
7172 pckgs = list (filter (lambda l : len (l ) > 0 , map (str .strip , f .read ().split ("\n " ))))
7273 return pckgs
7374
75+ def install_python_package_from_source (url , vcpkg , extractor , get_top_dir ):
76+ filename = os .path .basename (urlparse (url ).path )
77+ print ("Downloading %s --> %s" % (url , filename ))
78+ with urllib .request .urlopen (url ) as response , open (os .path .join ("SuperBuild" , "download" , filename ), 'wb' ) as out_file :
79+ shutil .copyfileobj (response , out_file )
80+
81+ print (f"Extracting { filename } " , end = "" )
82+ top_dir = None
83+ with extractor (os .path .join ("SuperBuild" , "download" , filename )) as z :
84+ top_dir = get_top_dir (z )
85+ z .extractall (os .path .join ("SuperBuild" , "src" ))
86+
87+ print (f" --> { top_dir } " )
88+ src_dir = os .path .join ("SuperBuild" , "src" , top_dir )
89+
90+ with open (os .path .join (src_dir , "setup.cfg" ), "w" ) as file :
91+ file .write ("[build_ext]\n " )
92+ file .write ("include-dirs = %s\n " % os .path .abspath (os .path .join (vcpkg , "include" )))
93+ file .write ("libraries = gdal\n " )
94+ file .write ("library-dirs = %s\n " % os .path .abspath (os .path .join (vcpkg , "lib" )))
95+
96+ pip_abs = os .path .abspath (os .path .join ("venv" , "Scripts" , "pip.exe" ))
97+
98+ run (f"\" { pip_abs } \" install ." , cwd = src_dir , env = {** os .environ , "GDAL_VERSION" : "3.11.1" })
99+
74100def build ():
75101 # Create python virtual env
76102 if not os .path .isdir ("venv" ):
77103 print ("Creating virtual env --> venv/" )
78104 ebuilder = EnvBuilder (with_pip = True )
79105 ebuilder .create ("venv" )
80106
81- run ("pip install setuptools" )
82- run ("venv\\ Scripts\\ pip install --ignore-installed -r requirements.txt" )
107+ run ("venv\\ Scripts\\ pip install setuptools" )
108+
109+ # Install numpy for OpenCV build. TODO: read the exact version from requirements.txt?
110+ run ("venv\\ Scripts\\ pip install numpy==2.3.2" )
83111
84112 # Download / build VCPKG environment
85113 if not os .path .isdir ("vcpkg" ):
@@ -114,9 +142,17 @@ def build():
114142 os .mkdir (build_dir )
115143
116144 toolchain_file = os .path .join (os .getcwd (), "vcpkg" , "scripts" , "buildsystems" , "vcpkg.cmake" )
117- run ("cmake .. -DCMAKE_TOOLCHAIN_FILE=\" %s\" " % toolchain_file , cwd = build_dir )
145+ run ("cmake .. -DCMAKE_BUILD_TYPE=Release - DCMAKE_TOOLCHAIN_FILE=\" %s\" " % toolchain_file , cwd = build_dir )
118146 run ("cmake --build . --config Release -j2" , cwd = build_dir )
119147
148+ # Build GDAL bindings, fiona and rasterio from source using vcpkg GDAL. TODO: read the exact versions from requirements.txt?
149+ vcpkg_installed = os .path .join (os .getcwd (), "vcpkg" , "installed" , "x64-windows" )
150+ install_python_package_from_source ("https://files.pythonhosted.org/packages/source/g/gdal/gdal-3.11.1.tar.gz" , vcpkg_installed , lambda path : tarfile .open (path , "r:gz" ), lambda t : t .getnames ()[0 ])
151+ install_python_package_from_source ("https://github.com/Toblerity/Fiona/archive/refs/tags/1.10.1.zip" , vcpkg_installed , lambda path : zipfile .ZipFile (path ), lambda z : z .namelist ()[0 ])
152+ install_python_package_from_source ("https://github.com/rasterio/rasterio/archive/refs/tags/1.4.3.zip" , vcpkg_installed , lambda path : zipfile .ZipFile (path ), lambda z : z .namelist ()[0 ])
153+
154+ run ("venv\\ Scripts\\ pip install -r requirements.txt" )
155+
120156def vcpkg_export ():
121157 if not os .path .exists ("vcpkg" ):
122158 print ("vcpkg directory does not exist. Did you build the environment?" )
@@ -168,7 +204,7 @@ def dist():
168204 # Download portable python
169205 if not os .path .isdir ("python312" ):
170206 pythonzip_path = os .path .join ("SuperBuild" , "download" , "python312.zip" )
171- python_url = "https://github.com/OpenDroneMap/windows-deps/releases/download/2.6 .0/python-3.12.10 -embed-amd64-less-pth.zip"
207+ python_url = "https://github.com/OpenDroneMap/windows-deps/releases/download/2.7 .0/python-3.12.9 -embed-amd64-less-pth-sqlite .zip"
172208 if not os .path .exists (pythonzip_path ):
173209 print ("Downloading %s" % python_url )
174210 with urllib .request .urlopen (python_url ) as response , open ( pythonzip_path , 'wb' ) as out_file :
0 commit comments