| 1 |
import setuptools |
| 2 |
from setuptools import Command, setup |
| 3 |
from setuptools.command.build import build |
| 4 |
import subprocess |
| 5 |
import glob, os, re, sys, string, platform |
| 6 |
|
| 7 |
# |
| 8 |
# REGEX for matching -I includes with or without quotes. |
| 9 |
# |
| 10 |
INCLUDE_REGEX = r"-I[\"']?([^\"']+)[\"']?" |
| 11 |
|
| 12 |
# these settings will read setup information from the environment. |
| 13 |
# instead of this, the relevant paths can be set directly here: |
| 14 |
# |
| 15 |
fltk_dir = "" |
| 16 |
opengl_dir = "" |
| 17 |
# this will be overrridden by environment variables: |
| 18 |
fltk_dir = os.environ.get('FLTK_HOME', '') |
| 19 |
opengl_dir = os.environ.get('OPENGL_HOME', '') |
| 20 |
|
| 21 |
#if environment vars are not set use fltk-config for all platforms but Windows |
| 22 |
if fltk_dir=="": |
| 23 |
if sys.platform != 'win32': |
| 24 |
try: |
| 25 |
fltk_dir=subprocess.check_output(['fltk-config','--prefix']).decode()[:-1] |
| 26 |
print(f'fltk dir found at {fltk_dir}') |
| 27 |
except: |
| 28 |
print('Error: fltk-config not found. Please install FLTK 1.4.x development files first and make sure fltk-config is in your PATH.') |
| 29 |
sys.exit() |
| 30 |
|
| 31 |
ver=subprocess.check_output(['fltk-config','--version']).decode()[:-1] |
| 32 |
if ver[:3] != '1.4': |
| 33 |
print(f'Error: FLTK development version {ver} found. Building pyFltk 1.4.x requires FLTK 1.4.x development files to be installed. Please uninstall previous FLTK development versions.') |
| 34 |
sys.exit() |
| 35 |
|
| 36 |
# Use this to pass additional compile flags, like macOS -mmacosx-version-min=OS_VERSION |
| 37 |
cxx_flags = os.environ.get('CXX_FLAGS', '') |
| 38 |
print("pyFLTK CXX_FLAGS=",cxx_flags) |
| 39 |
|
| 40 |
# add your .cpp extensions in the plug-in/src directory. |
| 41 |
UserDefinedSources = glob.glob('./plug-in/src/*.cpp') + \ |
| 42 |
glob.glob('./plug-in/src/*.cxx') |
| 43 |
# add additional include paths in the plug-in/include directory. |
| 44 |
UserIncludeDirs = ['./plug-in/include', './plug-in/src'] |
| 45 |
|
| 46 |
# do not edit beyond this point |
| 47 |
########################################################################### |
| 48 |
# command line configurations |
| 49 |
# --enable-shared (for Windows distributions that ship a single DLL) |
| 50 |
# --debug (for Windows distributions that change DLL name) |
| 51 |
# --disable-gl |
| 52 |
# --disable-forms |
| 53 |
########################################################################## |
| 54 |
doCheckForms = True |
| 55 |
doCheckGl = True |
| 56 |
isVerbose = True |
| 57 |
|
| 58 |
doMulti = False |
| 59 |
doOpenGL = False |
| 60 |
doForms = False |
| 61 |
doShared = False |
| 62 |
|
| 63 |
# debug flag |
| 64 |
doDebug = False |
| 65 |
new_args = [] |
| 66 |
for item in sys.argv: |
| 67 |
if item == '--disable-gl': |
| 68 |
doCheckGl = False |
| 69 |
if isVerbose: |
| 70 |
print("No OpenGL support!") |
| 71 |
elif item == '--disable-forms': |
| 72 |
doCheckForms = False |
| 73 |
if isVerbose: |
| 74 |
print("No Forms support") |
| 75 |
elif item == '--debug': |
| 76 |
doDebug = True |
| 77 |
if isVerbose: |
| 78 |
print("Detected DEBUG build!") |
| 79 |
elif item == '--enable-shared': |
| 80 |
doShared = True |
| 81 |
else: |
| 82 |
new_args.append(item) |
| 83 |
sys.argv = new_args |
| 84 |
|
| 85 |
########################################################################### |
| 86 |
# |
| 87 |
# create proper paths |
| 88 |
fltk_lib_dir = os.path.join(fltk_dir, 'lib') |
| 89 |
fltk_includes = [] |
| 90 |
compile_arg_list = [] |
| 91 |
link_arg_list = [] |
| 92 |
|
| 93 |
# whatever is platform dependent |
| 94 |
if sys.platform == 'win32': |
| 95 |
print("Building for MS Windows, using Visual C++") |
| 96 |
opengl_lib_dir = os.path.join(opengl_dir, 'lib') |
| 97 |
def_list = [('WIN32', '1'),('FL_INTERNALS','1')] |
| 98 |
compile_arg_list=['/GR', '/wd4101'] |
| 99 |
lib_dir_list = [fltk_lib_dir, opengl_lib_dir] |
| 100 |
win32_lib_list = ["kernel32", "user32", "gdi32", "winspool", "comdlg32", "Comctl32", "advapi32", "shell32", "ole32", "oleaut32", "uuid", "odbc32", "odbccp32", "wsock32", "gdiplus", "glu32", "opengl32"] |
| 101 |
if doDebug: |
| 102 |
static_lib_list = ["fltkd", "fltk_imagesd", "fltk_jpegd", "fltk_pngd", "fltk_zd", "fltk_formsd", "fltk_gld"] |
| 103 |
shared_lib_list = ["fltk_dlld"] |
| 104 |
else: |
| 105 |
static_lib_list = ["fltk", "fltk_images", "fltk_jpeg", "fltk_png", "fltk_z", "fltk_forms", "fltk_gl"] |
| 106 |
shared_lib_list = ["fltk_dll"] |
| 107 |
static_lib_list += win32_lib_list |
| 108 |
shared_lib_list += win32_lib_list |
| 109 |
if doShared: |
| 110 |
def_list = def_list + [('FL_DLL', 1)] |
| 111 |
lib_list = shared_lib_list |
| 112 |
else: |
| 113 |
lib_list = static_lib_list |
| 114 |
elif sys.platform.startswith('linux') or sys.platform.startswith('gnukfreebsd') or sys.platform.startswith('gnu0'): |
| 115 |
print("Building for Linux") |
| 116 |
|
| 117 |
def_list = [('UNIX', '1'),('FL_INTERNALS','1')] |
| 118 |
compile_arg_list.append('-Wno-unused-label') |
| 119 |
compile_arg_list.append('-Wno-unused-but-set-variable') |
| 120 |
compile_arg_list.append('-Wformat=1') |
| 121 |
compile_arg_list.append('-Werror=format-security') |
| 122 |
lib_dir_list = [fltk_lib_dir, '/usr/lib'] |
| 123 |
lib_list = ["fltk"] |
| 124 |
elif sys.platform in ['freebsd4','freebsd5','freebsd6','freebsd7', 'sunos5']: |
| 125 |
print(f"Building for: {sys.platform}") |
| 126 |
def_list = [('UNIX', '1'),('FL_INTERNALS','1')] |
| 127 |
lib_dir_list = [fltk_lib_dir,'/usr/X11R6/lib','/usr/lib'] |
| 128 |
lib_list = ["fltk"] |
| 129 |
elif sys.platform == 'darwin': |
| 130 |
print("Building for Mac OS X") |
| 131 |
def_list = [('UNIX', '1'),('FL_INTERNALS','1')] |
| 132 |
lib_dir_list = [fltk_lib_dir] |
| 133 |
lib_list = ["fltk"] |
| 134 |
cpu_type = platform.processor() |
| 135 |
if cpu_type.startswith("i386"): |
| 136 |
print("i386 CPU variant detected") |
| 137 |
lib_dir_list.append('/usr/local/lib') |
| 138 |
osx_arch = "x86_64" |
| 139 |
elif cpu_type.startswith("arm"): |
| 140 |
print("arm CPU variant detected") |
| 141 |
lib_dir_list.append('/opt/homebrew/lib') |
| 142 |
osx_arch = "arm64" |
| 143 |
else: |
| 144 |
print("PowerPC system detected") |
| 145 |
osx_arch = "ppc" |
| 146 |
|
| 147 |
compile_arg_list=['-arch', osx_arch] |
| 148 |
link_arg_list=['-stdlib=libc++', '-arch', osx_arch, '-framework','ApplicationServices','-framework','Carbon','-framework', 'Cocoa', '-framework','OpenGL','-framework','AGL','-rpath', fltk_lib_dir] |
| 149 |
|
| 150 |
else: |
| 151 |
print("Platform not officially supported!") |
| 152 |
print("You can try to edit the platform specific settings in the file setup.py by creating an entry for the following platform: ", sys.platform) |
| 153 |
sys.exit(0) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
########################################################################### |
| 158 |
# test for fltk configuration (libraries) |
| 159 |
def fltk_config(fltk_dir): |
| 160 |
global doMulti |
| 161 |
"return library paths and additional libraries that were used to link FLTK" |
| 162 |
needed_libraries = [] |
| 163 |
needed_directories = [] |
| 164 |
needed_includes = [] |
| 165 |
|
| 166 |
ver_cmd = None |
| 167 |
inc_cmd = None |
| 168 |
lib_cmd = None |
| 169 |
# always use images |
| 170 |
var_string = " --use-images" |
| 171 |
if doCheckGl: |
| 172 |
var_string = var_string + " --use-gl --use-glut" |
| 173 |
if doCheckForms: |
| 174 |
var_string = var_string + " --use-forms" |
| 175 |
try: |
| 176 |
#if isVerbose: |
| 177 |
# print("Checking fltk-config using FLTK_HOME") |
| 178 |
#fltk_dir = os.environ['FLTK_HOME'] |
| 179 |
if isVerbose: |
| 180 |
print(f"FLTK_HOME={fltk_dir}") |
| 181 |
ver_cmd = f"sh {fltk_dir}/bin/fltk-config --version" |
| 182 |
inc_cmd = f"sh {fltk_dir}/bin/fltk-config --cxxflags {var_string}" |
| 183 |
#lib_cmd = f"sh {fltk_dir}/bin/fltk-config --use-gl --use-glut --use-images --use-forms --ldflags" |
| 184 |
lib_cmd = f"sh {fltk_dir}/bin/fltk-config --ldflags {var_string}" |
| 185 |
except: |
| 186 |
if isVerbose: |
| 187 |
print("Checking fltk-config using default installation") |
| 188 |
|
| 189 |
ver_cmd = "fltk-config --version" |
| 190 |
inc_cmd = f"fltk-config --cxxflags {var_string}" |
| 191 |
lib_cmd = f"fltk-config --ldflags {var_string}" |
| 192 |
|
| 193 |
# version |
| 194 |
result = os.popen(ver_cmd).readlines() |
| 195 |
if len(result) == 0: |
| 196 |
print("No version information for FLTK found!") |
| 197 |
else: |
| 198 |
print("Using FLTK: ", result) |
| 199 |
|
| 200 |
# include flags |
| 201 |
result = os.popen(inc_cmd).readlines() |
| 202 |
if len(result) == 0: |
| 203 |
print("No compile flags found!") |
| 204 |
else: |
| 205 |
inc_list = map(lambda x: x.strip(), result[0].split(' ')) |
| 206 |
# inc_list = [x.strip() for x in result[0].split(' ')] |
| 207 |
|
| 208 |
for inc in inc_list: |
| 209 |
m = re.search(INCLUDE_REGEX, inc) |
| 210 |
if m: |
| 211 |
needed_includes.append(m.group(1)) |
| 212 |
if inc.find("_REENTRANT") >= 0: |
| 213 |
doMulti = True |
| 214 |
print("fltk-config includes: ", needed_includes) |
| 215 |
|
| 216 |
# lib flags |
| 217 |
result = os.popen(lib_cmd).readlines() |
| 218 |
if len(result) == 0: |
| 219 |
print("No link flags found!") |
| 220 |
else: |
| 221 |
lib_list = map(lambda x: x.strip(), result[0].split(' ')) |
| 222 |
# lib_list = [x.strip() for x in result[0].split(' ')] |
| 223 |
|
| 224 |
for lib in lib_list: |
| 225 |
if lib[:2] == '-l': |
| 226 |
needed_libraries.append(lib[2:]) |
| 227 |
if lib[:2] == '-L': |
| 228 |
library = lib[2:] |
| 229 |
if lib[2] == "'": |
| 230 |
library = lib[3:-1] |
| 231 |
needed_directories.append(library) |
| 232 |
print("fltk-config link paths: ", needed_directories) |
| 233 |
print("fltk-config link libraries: ", needed_libraries) |
| 234 |
|
| 235 |
return (needed_libraries, needed_directories, needed_includes) |
| 236 |
|
| 237 |
########################################################################### |
| 238 |
all_include_dirs = ['./src', './contrib','/usr/include'] |
| 239 |
if fltk_dir != "": |
| 240 |
#if (sys.platform == 'win32'): |
| 241 |
# all_include_dirs.insert(0, fltk_dir+"/include") |
| 242 |
#else: |
| 243 |
# all_include_dirs.insert(0, fltk_dir) |
| 244 |
all_include_dirs.insert(0, os.path.join(fltk_dir,'include')) |
| 245 |
print(all_include_dirs) |
| 246 |
########################################################################### |
| 247 |
|
| 248 |
if not (sys.platform == 'win32'): |
| 249 |
print("Checking FLTK configuration ... ") |
| 250 |
additional_libs, additional_dirs, additional_includes = fltk_config(fltk_dir) |
| 251 |
|
| 252 |
for item in additional_includes: |
| 253 |
# already included? |
| 254 |
add = True |
| 255 |
for used_inc in all_include_dirs: |
| 256 |
if item == used_inc: |
| 257 |
add = False |
| 258 |
break |
| 259 |
if add: |
| 260 |
all_include_dirs.insert(0, item) |
| 261 |
|
| 262 |
# check also for multi-threading |
| 263 |
pos = 0 |
| 264 |
for item in additional_libs: |
| 265 |
lowercase_item = item.lower() |
| 266 |
if lowercase_item.find("pthread") >= 0: |
| 267 |
doMulti = True |
| 268 |
if lowercase_item.find("fltk") >= 0 and lowercase_item.find("gl") >= 0: |
| 269 |
doOpenGL = True |
| 270 |
if lowercase_item.find("fltk") >= 0 and lowercase_item.find("forms") >= 0: |
| 271 |
doForms = True |
| 272 |
|
| 273 |
# simply add all the libraries to the front of the used libraries |
| 274 |
lib_list = additional_libs+lib_list |
| 275 |
|
| 276 |
if not doMulti: |
| 277 |
# disable multi-threading support |
| 278 |
print("FLTK was configured without multi-threading support!") |
| 279 |
compile_arg_list.append("-DDO_NOT_USE_THREADS") |
| 280 |
else: |
| 281 |
print("FLTK was configured with multi-threading support!") |
| 282 |
|
| 283 |
# On Mac OSX, openGL is always used for FLTK |
| 284 |
if sys.platform == 'darwin': |
| 285 |
doOpenGL = True |
| 286 |
|
| 287 |
if not doOpenGL: |
| 288 |
# disable OpenGL support |
| 289 |
print("FLTK was configured without OpenGL support!") |
| 290 |
UserDefinedSources.append('./src/Fl_Gl_Stubs.cxx') |
| 291 |
compile_arg_list.append("-DDO_NOT_USE_OPENGL") |
| 292 |
else: |
| 293 |
print("FLTK was configured with OpenGL support!") |
| 294 |
|
| 295 |
if not doForms: |
| 296 |
# disable Forms support |
| 297 |
print("FLTK was configured without Forms support!") |
| 298 |
UserDefinedSources.append('./src/Fl_Forms_Stubs.cxx') |
| 299 |
else: |
| 300 |
print("FLTK was configured with Forms support!") |
| 301 |
|
| 302 |
# add all the library paths |
| 303 |
lib_dir_list = additional_dirs+lib_dir_list |
| 304 |
|
| 305 |
|
| 306 |
########################################################################### |
| 307 |
|
| 308 |
class PySwigCommand(setuptools.Command): |
| 309 |
"""A custom command to run swig on the interface files.""" |
| 310 |
description = 'run swig on the interface files' |
| 311 |
user_options = [ |
| 312 |
# The format is (long option, short option, description). |
| 313 |
#('pylint-rcfile=', None, 'path to Pylint config file'), |
| 314 |
] |
| 315 |
include = [] |
| 316 |
|
| 317 |
def initialize_options(self): |
| 318 |
"""Set default values for options.""" |
| 319 |
# Each user option must be listed here with their default value. |
| 320 |
self.pylint_rcfile = '' |
| 321 |
|
| 322 |
def finalize_options(self): |
| 323 |
"""Post-process options.""" |
| 324 |
add_incl = [] |
| 325 |
try: |
| 326 |
fltk_dir = os.environ['FLTK_HOME'] |
| 327 |
#if (sys.platform == 'win32'): |
| 328 |
# add_incl.insert(0, f"-I{fltk_dir}/include") |
| 329 |
#else: |
| 330 |
# add_incl.insert(0, f"-I{fltk_dir}") |
| 331 |
add_incl.insert(0, f"-I{fltk_dir}/include") |
| 332 |
except: |
| 333 |
print("Using default location for FLTK!") |
| 334 |
result = os.popen('fltk-config --cxxflags').readlines() |
| 335 |
#print(result) |
| 336 |
if len(result) > 0: |
| 337 |
p_inc = map(lambda x: x.strip(), result[0].split(' ')) |
| 338 |
for item in p_inc: |
| 339 |
m = re.search(INCLUDE_REGEX, item) |
| 340 |
if m: |
| 341 |
add_incl.append('-I' + m.group(1)) |
| 342 |
else: |
| 343 |
print("FLTK not found!") |
| 344 |
add_incl.append('-I/usr/include') |
| 345 |
self.include = add_incl |
| 346 |
|
| 347 |
def run(self): |
| 348 |
"""Run command.""" |
| 349 |
command = ['swig', '-D{0}'.format(sys.platform.upper()), '-DFL_INTERNALS', '-w302', '-w312', '-w325', '-w362', '-w389', '-w401', '-w473', '-w509', '-I./swig', '-DPYTHON', '-DPYTHON3', '-c++', '-python', '-shadow', '-fastdispatch', '-outdir', 'fltk', '-o', 'fltk/fltk_wrap.cpp', './swig/fltk.i'] |
| 350 |
pos = command.index('-I./swig') |
| 351 |
if sys.platform.upper() == 'DARWIN': |
| 352 |
command[pos:pos] = ["-D__APPLE__"] |
| 353 |
pos += 2 |
| 354 |
else: |
| 355 |
pos +=1 |
| 356 |
command[pos:pos] = self.include |
| 357 |
self.announce( |
| 358 |
'Running command: %s' % str(command), |
| 359 |
level=3 |
| 360 |
) |
| 361 |
#subprocess.check_call(command, cwd='python') |
| 362 |
subprocess.check_call(command) |
| 363 |
|
| 364 |
if cxx_flags != '': |
| 365 |
compile_arg_list.append(cxx_flags) |
| 366 |
|
| 367 |
# module declarations |
| 368 |
contrib_sources = [] |
| 369 |
module1 = setuptools.Extension(name='fltk._fltk', |
| 370 |
define_macros=def_list, |
| 371 |
include_dirs = all_include_dirs+UserIncludeDirs, |
| 372 |
sources = ['./fltk/fltk_wrap.cpp', |
| 373 |
'./contrib/ListSelect.cpp']+UserDefinedSources, |
| 374 |
extra_compile_args=compile_arg_list, |
| 375 |
extra_link_args=link_arg_list, |
| 376 |
library_dirs=lib_dir_list, |
| 377 |
libraries=lib_list) |
| 378 |
|
| 379 |
class CustomBuild(build): |
| 380 |
sub_commands = [('build_custom', None)] + build.sub_commands |
| 381 |
|
| 382 |
# Note: all package metadata (name, version, description, authors, etc.) |
| 383 |
# is now defined in pyproject.toml. Only build logic lives here. |
| 384 |
setuptools.setup( |
| 385 |
cmdclass={ |
| 386 |
'build': CustomBuild, |
| 387 |
'build_custom': PySwigCommand, |
| 388 |
}, |
| 389 |
ext_modules=[module1], |
| 390 |
packages=['fltk', 'fltk.test'], |
| 391 |
package_data={'fltk': ['fltk']}, |
| 392 |
include_package_data=True, |
| 393 |
) |