3 ###################################
4 # Author: Kevin Ollivier
5 # Licence: wxWindows licence
6 ###################################
28 exitWithException
= True
29 nmakeCommand
= 'nmake.exe'
36 Detects the number of CPUs on a system.
37 This approach is from detectCPUs here: http://www.artima.com/weblogs/viewpost.jsp?thread=230001
39 # Linux, Unix and MacOS:
40 if hasattr(os
, "sysconf"):
41 if "SC_NPROCESSORS_ONLN" in os
.sysconf_names
:
43 ncpus
= os
.sysconf("SC_NPROCESSORS_ONLN")
44 if isinstance(ncpus
, int) and ncpus
> 0:
47 p
= subprocess
.Popen("sysctl -n hw.ncpu", shell
=True, stdout
=subprocess
.PIPE
)
48 return p
.stdout
.read()
51 if "NUMBER_OF_PROCESSORS" in os
.environ
:
52 ncpus
= int(os
.environ
["NUMBER_OF_PROCESSORS"]);
59 return getoutput("xcode-select -print-path")
63 text
= getoutput("cl.exe")
64 if 'Version 13' in text
:
66 if 'Version 15' in text
:
68 if 'Version 16' in text
:
70 # TODO: Add more tests to get the other versions...
75 def exitIfError(code
, msg
):
79 raise builder
.BuildError(msg
)
84 def getWxRelease(wxRoot
=None):
89 configureText
= open(os
.path
.join(wxRoot
, "configure.in"), "r").read()
90 majorVersion
= re
.search("wx_major_version_number=(\d+)", configureText
).group(1)
91 minorVersion
= re
.search("wx_minor_version_number=(\d+)", configureText
).group(1)
93 versionText
= "%s.%s" % (majorVersion
, minorVersion
)
95 if int(minorVersion
) % 2:
96 releaseVersion
= re
.search("wx_release_number=(\d+)", configureText
).group(1)
97 versionText
+= ".%s" % (releaseVersion
)
102 def getFrameworkName(options
):
103 # the name of the framework is based on the wx port being built
105 if options
.osx_cocoa
:
112 def getPrefixInFramework(options
, wxRoot
=None):
113 # the path inside the framework that is the wx --prefix
114 fwPrefix
= os
.path
.join(
115 os
.path
.abspath(options
.mac_framework_prefix
),
116 "%s.framework/Versions/%s" % (getFrameworkName(options
), getWxRelease(wxRoot
)))
120 def macFixupInstallNames(destdir
, prefix
, buildDir
=None):
121 # When an installdir is used then the install_names embedded in
122 # the dylibs are not correct. Reset the IDs and the dependencies
123 # to use just the prefix.
124 print("**** macFixupInstallNames(%s, %s, %s)" % (destdir
, prefix
, buildDir
))
126 os
.chdir(destdir
+prefix
+'/lib')
127 dylibs
= glob
.glob('*.dylib') # ('*[0-9].[0-9].[0-9].[0-9]*.dylib')
129 cmd
= 'install_name_tool -id %s/lib/%s %s/lib/%s' % \
130 (prefix
,lib
, destdir
+prefix
,lib
)
134 if buildDir
is not None:
135 cmd
= 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
136 (buildDir
,dep
, prefix
,dep
, destdir
+prefix
,lib
)
138 cmd
= 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
139 (destdir
+prefix
,dep
, prefix
,dep
, destdir
+prefix
,lib
)
148 print("Running %s" % cmd
)
149 return exitIfError(os
.system(cmd
), "Error running %s" % cmd
)
153 sp
= subprocess
.Popen(cmd
, shell
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
155 output
= sp
.stdout
.read()
156 if sys
.version_info
> (3,):
157 output
= output
.decode('utf-8') # TODO: is utf-8 okay here?
158 output
= output
.rstrip()
162 print("Command '%s' failed with exit code %d." % (cmd
, rval
))
167 def main(scriptName
, args
):
172 global configure_opts
176 scriptDir
= os
.path
.dirname(os
.path
.abspath(scriptName
))
177 wxRootDir
= os
.path
.abspath(os
.path
.join(scriptDir
, "..", ".."))
179 contribDir
= os
.path
.join("contrib", "src")
182 VERSION
= tuple([int(i
) for i
in getWxRelease().split('.')[:2]])
184 if sys
.platform
.startswith("win"):
185 contribDir
= os
.path
.join(wxRootDir
, "contrib", "build")
187 if sys
.platform
.startswith("win"):
192 defJobs
= str(numCPUs())
193 defFwPrefix
= '/Library/Frameworks'
196 "clean" : (False, "Clean all files from the build directory"),
197 "debug" : (False, "Build the library in debug symbols"),
198 "builddir" : ("", "Directory where the build will be performed for autoconf builds."),
199 "prefix" : ("", "Configured prefix to use for autoconf builds. Defaults to installdir if set. Ignored for framework builds."),
200 "jobs" : (defJobs
, "Number of jobs to run at one time in make. Default: %s" % defJobs
),
201 "install" : (False, "Install the toolkit to the installdir directory, or the default dir."),
202 "installdir" : ("", "Directory where built wxWidgets will be installed"),
203 "mac_distdir" : (None, "If set on Mac, will create an installer package in the specified dir."),
204 "mac_universal_binary"
205 : ("", "Comma separated list of architectures to include in the Mac universal binary"),
206 "mac_framework" : (False, "Install the Mac build as a framework"),
207 "mac_framework_prefix"
208 : (defFwPrefix
, "Prefix where the framework should be installed. Default: %s" % defFwPrefix
),
209 "cairo" : (False, "Enable dynamicly loading the Cairo lib for wxGraphicsContext on MSW"),
210 "no_config" : (False, "Turn off configure step on autoconf builds"),
211 "config_only" : (False, "Only run the configure step and then exit"),
212 "rebake" : (False, "Regenerate Bakefile and autoconf files"),
213 "unicode" : (False, "Build the library with unicode support"),
214 "wxpython" : (False, "Build the wxWidgets library with all options needed by wxPython"),
215 "cocoa" : (False, "Build the old Mac Cooca port."),
216 "osx_cocoa" : (False, "Build the new Cocoa port"),
217 "shared" : (False, "Build wx as a dynamic library"),
218 "extra_make" : ("", "Extra args to pass on [n]make's command line."),
219 "features" : ("", "A comma-separated list of wxUSE_XYZ defines on Win, or a list of configure flags on unix."),
220 "verbose" : (False, "Print commands as they are run, (to aid with debugging this script)"),
221 "jom" : (False, "Use jom.exe instead of nmake for MSW builds."),
224 parser
= optparse
.OptionParser(usage
="usage: %prog [options]", version
="%prog 1.0")
226 keys
= option_dict
.keys()
227 for opt
in sorted(keys
):
228 default
= option_dict
[opt
][0]
230 if type(default
) == bool:
231 action
= "store_true"
232 parser
.add_option("--" + opt
, default
=default
, action
=action
, dest
=opt
,
233 help=option_dict
[opt
][1])
235 options
, arguments
= parser
.parse_args(args
=args
)
241 # compiler / build system specific args
242 buildDir
= options
.builddir
244 installDir
= options
.installdir
245 prefixDir
= options
.prefix
247 if toolkit
== "autoconf":
249 buildDir
= os
.getcwd()
251 if options
.features
!= "":
252 configure_opts
.extend(options
.features
.split(" "))
255 configure_opts
.append("--enable-unicode")
258 configure_opts
.append("--enable-debug")
261 configure_opts
.append("--with-old_cocoa")
263 if options
.osx_cocoa
:
264 configure_opts
.append("--with-osx_cocoa")
266 wxpy_configure_opts
= [
269 "--enable-graphics_ctx",
270 "--enable-mediactrl",
273 "--enable-debug_flag",
275 "--disable-debugreport",
276 "--enable-uiactionsim",
279 if sys
.platform
.startswith("darwin"):
280 wxpy_configure_opts
.append("--enable-monolithic")
282 wxpy_configure_opts
.append("--with-sdl")
283 wxpy_configure_opts
.append("--with-gnomeprint")
285 # Try to use use lowest available SDK back to 10.5. Both Carbon and
286 # Cocoa builds require at least the 10.5 SDK now. We only add it to
287 # the wxpy options because this is a hard-requirement for wxPython,
288 # but other cases it is optional and is left up to the developer.
289 # TODO: there should be a command line option to set the SDK...
290 if sys
.platform
.startswith("darwin"):
291 xcodePath
= getXcodePath()
293 xcodePath
+"/SDKs/MacOSX10.5.sdk",
294 xcodePath
+"/SDKs/MacOSX10.6.sdk",
295 xcodePath
+"/SDKs/MacOSX10.7.sdk",
298 # use the lowest available sdk
300 if os
.path
.exists(sdk
):
301 wxpy_configure_opts
.append(
302 "--with-macosx-sdk=%s" % sdk
)
305 if not options
.mac_framework
:
306 if installDir
and not prefixDir
:
307 prefixDir
= installDir
309 prefixDir
= os
.path
.abspath(prefixDir
)
310 configure_opts
.append("--prefix=" + prefixDir
)
314 configure_opts
.extend(wxpy_configure_opts
)
316 # wxPython likes adding these debug options too
317 configure_opts
.append("--enable-debug_gdb")
318 configure_opts
.append("--disable-optimise")
319 configure_opts
.remove("--enable-optimise")
323 retval
= run("make -f autogen.mk")
324 exitIfError(retval
, "Error running autogen.mk")
326 if options
.mac_framework
:
327 # TODO: Should options.install be automatically turned on if the
328 # mac_framework flag is given?
330 # framework builds always need to be monolithic
331 if not "--enable-monolithic" in configure_opts
:
332 configure_opts
.append("--enable-monolithic")
334 # The --prefix given to configure will be the framework prefix
335 # plus the framework specific dir structure.
336 prefixDir
= getPrefixInFramework(options
)
337 configure_opts
.append("--prefix=" + prefixDir
)
339 # the framework build adds symlinks above the installDir + prefixDir folder
340 # so we need to wipe from the framework root instead of inside the prefixDir.
341 frameworkRootDir
= os
.path
.abspath(os
.path
.join(installDir
+ prefixDir
, "..", ".."))
342 if os
.path
.exists(frameworkRootDir
):
343 if os
.path
.exists(frameworkRootDir
):
344 shutil
.rmtree(frameworkRootDir
)
346 if options
.mac_universal_binary
:
347 if options
.mac_universal_binary
== 'default':
348 if options
.osx_cocoa
:
349 configure_opts
.append("--enable-universal_binary=i386,x86_64")
351 configure_opts
.append("--enable-universal_binary")
353 configure_opts
.append("--enable-universal_binary=%s" % options
.mac_universal_binary
)
356 print("Configure options: " + repr(configure_opts
))
357 wxBuilder
= builder
.AutoconfBuilder()
358 if not options
.no_config
and not options
.clean
:
362 exitIfError(wxBuilder
.configure(dir=wxRootDir
, options
=configure_opts
),
363 "Error running configure")
366 if options
.config_only
:
367 print("Exiting after configure")
370 elif toolkit
in ["msvc", "msvcProject"]:
372 buildDir
= os
.path
.abspath(os
.path
.join(scriptDir
, "..", "msw"))
374 print("creating wx/msw/setup.h from setup0.h")
376 flags
["wxUSE_UNICODE"] = "1"
378 flags
["wxUSE_UNICODE_MSLU"] = "1"
381 if not os
.environ
.get("CAIRO_ROOT"):
382 print("WARNING: Expected CAIRO_ROOT set in the environment!")
383 flags
["wxUSE_CAIRO"] = "1"
386 flags
["wxDIALOG_UNIT_COMPATIBILITY "] = "0"
387 flags
["wxUSE_DEBUGREPORT"] = "0"
388 flags
["wxUSE_DIALUP_MANAGER"] = "0"
389 flags
["wxUSE_GRAPHICS_CONTEXT"] = "1"
390 flags
["wxUSE_DISPLAY"] = "1"
391 flags
["wxUSE_GLCANVAS"] = "1"
392 flags
["wxUSE_POSTSCRIPT"] = "1"
393 flags
["wxUSE_AFM_FOR_POSTSCRIPT"] = "0"
394 flags
["wxUSE_DATEPICKCTRL_GENERIC"] = "1"
397 flags
["wxUSE_DIB_FOR_BITMAP"] = "1"
400 flags
["wxUSE_UIACTIONSIMULATOR"] = "1"
403 mswIncludeDir
= os
.path
.join(wxRootDir
, "include", "wx", "msw")
404 setup0File
= os
.path
.join(mswIncludeDir
, "setup0.h")
405 setupText
= open(setup0File
, "rb").read()
408 setupText
, subsMade
= re
.subn(flag
+ "\s+?\d", "%s %s" % (flag
, flags
[flag
]), setupText
)
410 print("Flag %s wasn't found in setup0.h!" % flag
)
413 setupFile
= open(os
.path
.join(mswIncludeDir
, "setup.h"), "wb")
414 setupFile
.write(setupText
)
417 if toolkit
== "msvc":
418 print("setting build options...")
419 args
.append("-f makefile.vc")
421 args
.append("UNICODE=1")
423 args
.append("MSLU=1")
426 args
.append("OFFICIAL_BUILD=1")
427 args
.append("COMPILER_VERSION=%s" % getVisCVersion())
428 args
.append("SHARED=1")
429 args
.append("MONOLITHIC=0")
430 args
.append("USE_OPENGL=1")
431 args
.append("USE_GDIPLUS=1")
433 if not options
.debug
:
434 args
.append("BUILD=release")
436 args
.append("BUILD=debug")
439 args
.append("SHARED=1")
444 os
.path
.join(os
.environ
.get("CAIRO_ROOT", ""), 'include\\cairo'))
447 nmakeCommand
= 'jom.exe'
449 wxBuilder
= builder
.MSVCBuilder(commandName
=nmakeCommand
)
451 if toolkit
== "msvcProject":
453 if options
.shared
or options
.wxpython
:
454 args
.append("wx_dll.dsw")
456 args
.append("wx.dsw")
459 wxBuilder
= builder
.MSVCProjectBuilder()
463 print("Builder not available for your specified platform/compiler.")
467 print("Performing cleanup.")
468 wxBuilder
.clean(dir=buildDir
, options
=args
)
472 if options
.extra_make
:
473 args
.append(options
.extra_make
)
475 if not sys
.platform
.startswith("win"):
476 args
.append("--jobs=" + options
.jobs
)
477 exitIfError(wxBuilder
.build(dir=buildDir
, options
=args
), "Error building")
482 extra
= ['DESTDIR='+installDir
]
483 wxBuilder
.install(dir=buildDir
, options
=extra
)
485 if options
.install
and options
.mac_framework
:
487 def renameLibrary(libname
, frameworkname
):
490 while os
.path
.islink(reallib
):
491 links
.append(reallib
)
492 reallib
= "lib/" + os
.readlink(reallib
)
494 #print("reallib is %s" % reallib)
495 run("mv -f %s lib/%s.dylib" % (reallib
, frameworkname
))
498 run("ln -s -f %s.dylib %s" % (frameworkname
, link
))
500 frameworkRootDir
= prefixDir
502 print("installDir = %s" % installDir
)
503 frameworkRootDir
= installDir
+ prefixDir
504 os
.chdir(frameworkRootDir
)
509 fwname
= getFrameworkName(options
)
510 version
= getoutput("bin/wx-config --release")
511 version_full
= getoutput("bin/wx-config --version")
512 basename
= getoutput("bin/wx-config --basename")
513 configname
= getoutput("bin/wx-config --selected-config")
515 os
.makedirs("Resources")
517 CFBundleDevelopmentRegion
="English",
518 CFBundleIdentifier
='org.wxwidgets.wxosxcocoa',
520 CFBundleVersion
=version_full
,
521 CFBundleExecutable
=fwname
,
522 CFBundleGetInfoString
="%s %s" % (fwname
, version_full
),
523 CFBundlePackageType
="FMWK",
524 CFBundleSignature
="WXCO",
525 CFBundleShortVersionString
=version_full
,
526 CFBundleInfoDictionaryVersion
="6.0",
530 plistlib
.writePlist(wxplist
, os
.path
.join(frameworkRootDir
, "Resources", "Info.plist"))
532 # we make wx the "actual" library file and link to it from libwhatever.dylib
533 # so that things can link to wx and survive minor version changes
534 renameLibrary("lib/lib%s-%s.dylib" % (basename
, version
), fwname
)
535 run("ln -s -f lib/%s.dylib %s" % (fwname
, fwname
))
537 run("ln -s -f include Headers")
539 for lib
in ["GL", "STC", "Gizmos", "Gizmos_xrc"]:
540 libfile
= "lib/lib%s_%s-%s.dylib" % (basename
, lib
.lower(), version
)
541 if os
.path
.exists(libfile
):
542 frameworkDir
= "framework/wx%s/%s" % (lib
, version
)
543 if not os
.path
.exists(frameworkDir
):
544 os
.makedirs(frameworkDir
)
545 renameLibrary(libfile
, "wx" + lib
)
546 run("ln -s -f ../../../%s %s/wx%s" % (libfile
, frameworkDir
, lib
))
548 for lib
in glob
.glob("lib/*.dylib"):
549 if not os
.path
.islink(lib
):
550 corelibname
= "lib/lib%s-%s.0.dylib" % (basename
, version
)
551 run("install_name_tool -id %s %s" % (os
.path
.join(prefixDir
, lib
), lib
))
552 run("install_name_tool -change %s %s %s" % (os
.path
.join(frameworkRootDir
, corelibname
), os
.path
.join(prefixDir
, corelibname
), lib
))
556 header_template
= """
557 #ifndef __WX_FRAMEWORK_HEADER__
558 #define __WX_FRAMEWORK_HEADER__
562 #endif // __WX_FRAMEWORK_HEADER__
565 header_dir
= "wx-%s/wx" % version
566 for include
in glob
.glob(header_dir
+ "/*.h"):
567 headers
+= "#include <wx/" + os
.path
.basename(include
) + ">\n"
569 framework_header
= open("%s.h" % fwname
, "w")
570 framework_header
.write(header_template
% headers
)
571 framework_header
.close()
573 run("ln -s -f %s wx" % header_dir
)
574 os
.chdir("wx-%s/wx" % version
)
575 run("ln -s -f ../../../lib/wx/include/%s/wx/setup.h setup.h" % configname
)
577 os
.chdir(os
.path
.join(frameworkRootDir
, ".."))
578 run("ln -s -f %s Current" % getWxRelease())
580 run("ln -s -f Versions/Current/Headers Headers")
581 run("ln -s -f Versions/Current/Resources Resources")
582 run("ln -s -f Versions/Current/%s %s" % (fwname
, fwname
))
584 # sanity check to ensure the symlink works
585 os
.chdir("Versions/Current")
587 # put info about the framework into wx-config
588 os
.chdir(frameworkRootDir
)
589 text
= file('lib/wx/config/%s' % configname
).read()
590 text
= text
.replace("MAC_FRAMEWORK=", "MAC_FRAMEWORK=%s" % getFrameworkName(options
))
591 if options
.mac_framework_prefix
not in ['/Library/Frameworks',
592 '/System/Library/Frameworks']:
593 text
= text
.replace("MAC_FRAMEWORK_PREFIX=",
594 "MAC_FRAMEWORK_PREFIX=%s" % options
.mac_framework_prefix
)
595 file('lib/wx/config/%s' % configname
, 'w').write(text
)
597 # The framework is finished!
598 print("wxWidgets framework created at: " +
599 os
.path
.join( installDir
,
600 options
.mac_framework_prefix
,
601 '%s.framework' % fwname
))
604 # adjust the install_name if needed
605 if sys
.platform
.startswith("darwin") and \
606 options
.install
and \
607 options
.installdir
and \
608 not options
.mac_framework
and \
609 not options
.wxpython
: # wxPython's build will do this later if needed
611 prefixDir
= '/usr/local'
612 macFixupInstallNames(options
.installdir
, prefixDir
)#, buildDir)
614 # make a package if a destdir was set.
615 if options
.mac_framework
and \
616 options
.install
and \
617 options
.installdir
and \
620 if os
.path
.exists(options
.mac_distdir
):
621 shutil
.rmtree(options
.mac_distdir
)
623 packagedir
= os
.path
.join(options
.mac_distdir
, "packages")
624 os
.makedirs(packagedir
)
625 basename
= os
.path
.basename(prefixDir
.split(".")[0])
626 packageName
= basename
+ "-" + getWxRelease()
627 packageMakerPath
= getXcodePath()+"/usr/bin/packagemaker "
629 args
.append("--root %s" % options
.installdir
)
630 args
.append("--id org.wxwidgets.%s" % basename
.lower())
631 args
.append("--title %s" % packageName
)
632 args
.append("--version %s" % getWxRelease())
633 args
.append("--out %s" % os
.path
.join(packagedir
, packageName
+ ".pkg"))
634 cmd
= packageMakerPath
+ ' '.join(args
)
635 print("cmd = %s" % cmd
)
638 os
.chdir(options
.mac_distdir
)
640 run('hdiutil create -srcfolder %s -volname "%s" -imagekey zlib-level=9 %s.dmg' % (packagedir
, packageName
, packageName
))
642 shutil
.rmtree(packagedir
)
644 if __name__
== '__main__':
645 exitWithException
= False # use sys.exit instead
646 main(sys
.argv
[0], sys
.argv
[1:])