3 ###################################
4 # Author: Kevin Ollivier
5 # Licence: wxWindows licence
6 ###################################
29 exitWithException
= True
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 os
.sysconf_names
.has_key("SC_NPROCESSORS_ONLN"):
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 os
.environ
.has_key("NUMBER_OF_PROCESSORS"):
52 ncpus
= int(os
.environ
["NUMBER_OF_PROCESSORS"]);
58 def exitIfError(code
, msg
):
62 raise builder
.BuildError
, msg
67 def getWxRelease(wxRoot
=None):
72 configureText
= open(os
.path
.join(wxRoot
, "configure.in"), "r").read()
73 majorVersion
= re
.search("wx_major_version_number=(\d+)", configureText
).group(1)
74 minorVersion
= re
.search("wx_minor_version_number=(\d+)", configureText
).group(1)
76 versionText
= "%s.%s" % (majorVersion
, minorVersion
)
78 if int(minorVersion
) % 2:
79 releaseVersion
= re
.search("wx_release_number=(\d+)", configureText
).group(1)
80 versionText
+= ".%s" % (releaseVersion
)
85 def getFrameworkName(options
):
86 # the name of the framework is based on the wx port being built
95 def getPrefixInFramework(options
, wxRoot
=None):
96 # the path inside the framework that is the wx --prefix
97 fwPrefix
= os
.path
.join(
98 os
.path
.abspath(options
.mac_framework_prefix
),
99 "%s.framework/Versions/%s" % (getFrameworkName(options
), getWxRelease(wxRoot
)))
103 def macFixupInstallNames(destdir
, prefix
, buildDir
=None):
104 # When an installdir is used then the install_names embedded in
105 # the dylibs are not correct. Reset the IDs and the dependencies
106 # to use just the prefix.
107 print "**** macFixupInstallNames(%s, %s, %s)" % (destdir
, prefix
, buildDir
)
109 os
.chdir(destdir
+prefix
+'/lib')
110 dylibs
= glob
.glob('*.dylib') # ('*[0-9].[0-9].[0-9].[0-9]*.dylib')
112 cmd
= 'install_name_tool -id %s/lib/%s %s/lib/%s' % \
113 (prefix
,lib
, destdir
+prefix
,lib
)
117 if buildDir
is not None:
118 cmd
= 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
119 (buildDir
,dep
, prefix
,dep
, destdir
+prefix
,lib
)
121 cmd
= 'install_name_tool -change %s/lib/%s %s/lib/%s %s/lib/%s' % \
122 (destdir
+prefix
,dep
, prefix
,dep
, destdir
+prefix
,lib
)
131 print "Running %s" % cmd
132 return exitIfError(os
.system(cmd
), "Error running %s" % cmd
)
135 def main(scriptName
, args
):
140 global configure_opts
143 scriptDir
= os
.path
.dirname(os
.path
.abspath(scriptName
))
144 wxRootDir
= os
.path
.abspath(os
.path
.join(scriptDir
, "..", ".."))
146 contribDir
= os
.path
.join("contrib", "src")
149 VERSION
= tuple([int(i
) for i
in getWxRelease().split('.')[:2]])
151 if sys
.platform
.startswith("win"):
152 contribDir
= os
.path
.join(wxRootDir
, "contrib", "build")
154 if sys
.platform
.startswith("win"):
159 defJobs
= str(numCPUs())
160 defFwPrefix
= '/Library/Frameworks'
163 "clean" : (False, "Clean all files from the build directory"),
164 "debug" : (False, "Build the library in debug symbols"),
165 "builddir" : ("", "Directory where the build will be performed for autoconf builds."),
166 "prefix" : ("", "Configured prefix to use for autoconf builds. Defaults to installdir if set. Ignored for framework builds."),
167 "jobs" : (defJobs
, "Number of jobs to run at one time in make. Default: %s" % defJobs
),
168 "install" : (False, "Install the toolkit to the installdir directory, or the default dir."),
169 "installdir" : ("", "Directory where built wxWidgets will be installed"),
170 "mac_distdir" : (None, "If set on Mac, will create an installer package in the specified dir."),
171 "mac_universal_binary"
172 : (False, "Build Mac version as a universal binary"),
173 "mac_arch" : ("", "Build just the specified architecture on Mac"),
174 "mac_framework" : (False, "Install the Mac build as a framework"),
175 "mac_framework_prefix"
176 : (defFwPrefix
, "Prefix where the framework should be installed. Default: %s" % defFwPrefix
),
177 "no_config" : (False, "Turn off configure step on autoconf builds"),
178 "config_only" : (False, "Only run the configure step and then exit"),
179 "rebake" : (False, "Regenerate Bakefile and autoconf files"),
180 "unicode" : (False, "Build the library with unicode support"),
181 "wxpython" : (False, "Build the wxWidgets library with all options needed by wxPython"),
182 "cocoa" : (False, "Build the old Mac Cooca port."),
183 "osx_cocoa" : (False, "Build the new Cocoa port"),
184 "shared" : (False, "Build wx as a dynamic library"),
185 "cairo" : (False, "Build support for wxCairoContext (always true on GTK+)"),
186 "extra_make" : ("", "Extra args to pass on [n]make's command line."),
187 "features" : ("", "A comma-separated list of wxUSE_XYZ defines on Win, or a list of configure flags on unix."),
188 "verbose" : (False, "Print commands as they are run, (to aid with debugging this script)"),
191 parser
= optparse
.OptionParser(usage
="usage: %prog [options]", version
="%prog 1.0")
193 keys
= option_dict
.keys()
196 default
= option_dict
[opt
][0]
198 if type(default
) == types
.BooleanType
:
199 action
= "store_true"
200 parser
.add_option("--" + opt
, default
=default
, action
=action
, dest
=opt
,
201 help=option_dict
[opt
][1])
203 options
, arguments
= parser
.parse_args(args
=args
)
209 # compiler / build system specific args
210 buildDir
= options
.builddir
212 installDir
= options
.installdir
213 prefixDir
= options
.prefix
215 if toolkit
== "autoconf":
217 buildDir
= os
.getcwd()
219 if options
.features
!= "":
220 configure_opts
.extend(options
.features
.split(" "))
223 configure_opts
.append("--enable-unicode")
226 configure_opts
.append("--enable-debug")
229 configure_opts
.append("--with-old_cocoa")
231 if options
.osx_cocoa
:
232 configure_opts
.append("--with-osx_cocoa")
235 configure_opts
.append("--enable-macosx_arch=%s" % options
.mac_arch
)
237 wxpy_configure_opts
= [
240 "--enable-graphics_ctx",
241 "--enable-mediactrl",
244 "--enable-debug_flag",
246 "--disable-debugreport",
247 "--enable-uiactionsim",
250 if sys
.platform
.startswith("darwin"):
251 wxpy_configure_opts
.append("--enable-monolithic")
253 wxpy_configure_opts
.append("--with-sdl")
254 wxpy_configure_opts
.append("--with-gnomeprint")
256 # Ensure that the Carbon build stays compatible back to 10.4 and
257 # for the Cocoa build allow running on 10.5 and newer. We only add
258 # them to the wxpy options because this is a hard-requirement for
259 # wxPython, but other cases it is optional and is left up to the
260 # developer. TODO: there should be a command line option to set
262 if sys
.platform
.startswith("darwin"):
263 if not options
.osx_cocoa
:
264 wxpy_configure_opts
.append(
265 "--with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk")
267 wxpy_configure_opts
.append(
268 "--with-macosx-sdk=/Developer/SDKs/MacOSX10.5.sdk")
271 if not options
.mac_framework
:
272 if installDir
and not prefixDir
:
273 prefixDir
= installDir
275 prefixDir
= os
.path
.abspath(prefixDir
)
276 configure_opts
.append("--prefix=" + prefixDir
)
280 configure_opts
.extend(wxpy_configure_opts
)
282 # wxPython likes adding these debug options too
283 configure_opts
.append("--enable-debug_gdb")
284 configure_opts
.append("--disable-optimise")
285 configure_opts
.remove("--enable-optimise")
289 retval
= run("make -f autogen.mk")
290 exitIfError(retval
, "Error running autogen.mk")
292 if options
.mac_framework
:
293 # TODO: Should options.install be automatically turned on if the
294 # mac_framework flag is given?
296 # The framework build is always a universal binary, unless we are
297 # explicitly told to build only one architecture
298 if not options
.mac_arch
:
299 options
.mac_universal_binary
= True
301 # framework builds always need to be monolithic
302 if not "--enable-monolithic" in configure_opts
:
303 configure_opts
.append("--enable-monolithic")
305 # The --prefix given to configure will be the framework prefix
306 # plus the framework specific dir structure.
307 prefixDir
= getPrefixInFramework(options
)
308 configure_opts
.append("--prefix=" + prefixDir
)
310 # the framework build adds symlinks above the installDir + prefixDir folder
311 # so we need to wipe from the framework root instead of inside the prefixDir.
312 frameworkRootDir
= os
.path
.abspath(os
.path
.join(installDir
+ prefixDir
, "..", ".."))
313 if os
.path
.exists(frameworkRootDir
):
314 if os
.path
.exists(frameworkRootDir
):
315 shutil
.rmtree(frameworkRootDir
)
317 if options
.mac_universal_binary
:
318 configure_opts
.append("--enable-universal_binary")
321 print "Configure options: " + `configure_opts`
322 wxBuilder
= builder
.AutoconfBuilder()
323 if not options
.no_config
and not options
.clean
:
327 exitIfError(wxBuilder
.configure(dir=wxRootDir
, options
=configure_opts
),
328 "Error running configure")
331 if options
.config_only
:
332 print "Exiting after configure"
335 elif toolkit
in ["msvc", "msvcProject"]:
337 buildDir
= os
.path
.abspath(os
.path
.join(scriptDir
, "..", "msw"))
339 print "creating wx/msw/setup.h from setup0.h"
341 flags
["wxUSE_UNICODE"] = "1"
343 flags
["wxUSE_UNICODE_MSLU"] = "1"
346 flags
["wxUSE_CAIRO"] = "1"
349 flags
["wxDIALOG_UNIT_COMPATIBILITY "] = "0"
350 flags
["wxUSE_DEBUGREPORT"] = "0"
351 flags
["wxUSE_DIALUP_MANAGER"] = "0"
352 flags
["wxUSE_GRAPHICS_CONTEXT"] = "1"
353 flags
["wxUSE_DISPLAY"] = "1"
354 flags
["wxUSE_GLCANVAS"] = "1"
355 flags
["wxUSE_POSTSCRIPT"] = "1"
356 flags
["wxUSE_AFM_FOR_POSTSCRIPT"] = "0"
357 flags
["wxUSE_DATEPICKCTRL_GENERIC"] = "1"
360 flags
["wxUSE_DIB_FOR_BITMAP"] = "1"
363 flags
["wxUSE_UIACTIONSIMULATOR"] = "1"
366 mswIncludeDir
= os
.path
.join(wxRootDir
, "include", "wx", "msw")
367 setup0File
= os
.path
.join(mswIncludeDir
, "setup0.h")
368 setupText
= open(setup0File
, "rb").read()
371 setupText
, subsMade
= re
.subn(flag
+ "\s+?\d", "%s %s" % (flag
, flags
[flag
]), setupText
)
373 print "Flag %s wasn't found in setup0.h!" % flag
376 setupFile
= open(os
.path
.join(mswIncludeDir
, "setup.h"), "wb")
377 setupFile
.write(setupText
)
380 if toolkit
== "msvc":
381 print "setting build options..."
382 args
.append("-f makefile.vc")
384 args
.append("UNICODE=1")
386 args
.append("MSLU=1")
389 args
.append("OFFICIAL_BUILD=1")
390 args
.append("SHARED=1")
391 args
.append("MONOLITHIC=0")
392 args
.append("USE_OPENGL=1")
393 args
.append("USE_GDIPLUS=1")
395 if not options
.debug
:
396 args
.append("BUILD=release")
398 args
.append("BUILD=debug")
401 args
.append("SHARED=1")
403 args
.append("USE_CAIRO=1")
405 wxBuilder
= builder
.MSVCBuilder()
407 if toolkit
== "msvcProject":
409 if options
.shared
or options
.wxpython
:
410 args
.append("wx_dll.dsw")
412 args
.append("wx.dsw")
415 wxBuilder
= builder
.MSVCProjectBuilder()
419 print "Builder not available for your specified platform/compiler."
423 print "Performing cleanup."
424 wxBuilder
.clean(dir=buildDir
, options
=args
)
428 if options
.extra_make
:
429 args
.append(options
.extra_make
)
431 if not sys
.platform
.startswith("win"):
432 args
.append("--jobs=" + options
.jobs
)
433 exitIfError(wxBuilder
.build(dir=buildDir
, options
=args
), "Error building")
438 extra
= ['DESTDIR='+installDir
]
439 wxBuilder
.install(dir=buildDir
, options
=extra
)
441 if options
.install
and options
.mac_framework
:
443 def renameLibrary(libname
, frameworkname
):
446 while os
.path
.islink(reallib
):
447 links
.append(reallib
)
448 reallib
= "lib/" + os
.readlink(reallib
)
450 #print "reallib is %s" % reallib
451 run("mv -f %s lib/%s.dylib" % (reallib
, frameworkname
))
454 run("ln -s -f %s.dylib %s" % (frameworkname
, link
))
456 frameworkRootDir
= prefixDir
458 print "installDir = %s" % installDir
459 frameworkRootDir
= installDir
+ prefixDir
460 os
.chdir(frameworkRootDir
)
465 fwname
= getFrameworkName(options
)
466 version
= commands
.getoutput("bin/wx-config --release")
467 version_full
= commands
.getoutput("bin/wx-config --version")
468 basename
= commands
.getoutput("bin/wx-config --basename")
469 configname
= commands
.getoutput("bin/wx-config --selected-config")
471 os
.makedirs("Resources")
473 CFBundleDevelopmentRegion
="English",
474 CFBundleIdentifier
='org.wxwidgets.wxosxcocoa',
476 CFBundleVersion
=version_full
,
477 CFBundleExecutable
=fwname
,
478 CFBundleGetInfoString
="%s %s" % (fwname
, version_full
),
479 CFBundlePackageType
="FMWK",
480 CFBundleSignature
="WXCO",
481 CFBundleShortVersionString
=version_full
,
482 CFBundleInfoDictionaryVersion
="6.0",
486 plistlib
.writePlist(wxplist
, os
.path
.join(frameworkRootDir
, "Resources", "Info.plist"))
488 # we make wx the "actual" library file and link to it from libwhatever.dylib
489 # so that things can link to wx and survive minor version changes
490 renameLibrary("lib/lib%s-%s.dylib" % (basename
, version
), fwname
)
491 run("ln -s -f lib/%s.dylib %s" % (fwname
, fwname
))
493 run("ln -s -f include Headers")
495 for lib
in ["GL", "STC", "Gizmos", "Gizmos_xrc"]:
496 libfile
= "lib/lib%s_%s-%s.dylib" % (basename
, lib
.lower(), version
)
497 if os
.path
.exists(libfile
):
498 frameworkDir
= "framework/wx%s/%s" % (lib
, version
)
499 if not os
.path
.exists(frameworkDir
):
500 os
.makedirs(frameworkDir
)
501 renameLibrary(libfile
, "wx" + lib
)
502 run("ln -s -f ../../../%s %s/wx%s" % (libfile
, frameworkDir
, lib
))
504 for lib
in glob
.glob("lib/*.dylib"):
505 if not os
.path
.islink(lib
):
506 corelibname
= "lib/lib%s-%s.0.dylib" % (basename
, version
)
507 run("install_name_tool -id %s %s" % (os
.path
.join(prefixDir
, lib
), lib
))
508 run("install_name_tool -change %s %s %s" % (os
.path
.join(frameworkRootDir
, corelibname
), os
.path
.join(prefixDir
, corelibname
), lib
))
512 header_template
= """
513 #ifndef __WX_FRAMEWORK_HEADER__
514 #define __WX_FRAMEWORK_HEADER__
518 #endif // __WX_FRAMEWORK_HEADER__
521 header_dir
= "wx-%s/wx" % version
522 for include
in glob
.glob(header_dir
+ "/*.h"):
523 headers
+= "#include <wx/" + os
.path
.basename(include
) + ">\n"
525 framework_header
= open("%s.h" % fwname
, "w")
526 framework_header
.write(header_template
% headers
)
527 framework_header
.close()
529 run("ln -s -f %s wx" % header_dir
)
530 os
.chdir("wx-%s/wx" % version
)
531 run("ln -s -f ../../../lib/wx/include/%s/wx/setup.h setup.h" % configname
)
533 os
.chdir(os
.path
.join(frameworkRootDir
, ".."))
534 run("ln -s -f %s Current" % getWxRelease())
536 run("ln -s -f Versions/Current/Headers Headers")
537 run("ln -s -f Versions/Current/Resources Resources")
538 run("ln -s -f Versions/Current/%s %s" % (fwname
, fwname
))
540 # sanity check to ensure the symlink works
541 os
.chdir("Versions/Current")
543 # put info about the framework into wx-config
544 os
.chdir(frameworkRootDir
)
545 text
= file('lib/wx/config/%s' % configname
).read()
546 text
= text
.replace("MAC_FRAMEWORK=", "MAC_FRAMEWORK=%s" % getFrameworkName(options
))
547 if options
.mac_framework_prefix
not in ['/Library/Frameworks',
548 '/System/Library/Frameworks']:
549 text
= text
.replace("MAC_FRAMEWORK_PREFIX=",
550 "MAC_FRAMEWORK_PREFIX=%s" % options
.mac_framework_prefix
)
551 file('lib/wx/config/%s' % configname
, 'w').write(text
)
553 # The framework is finished!
554 print "wxWidgets framework created at: " + \
555 os
.path
.join( installDir
,
556 options
.mac_framework_prefix
,
557 '%s.framework' % fwname
)
560 # adjust the install_name if needed
561 if sys
.platform
.startswith("darwin") and \
562 options
.install
and \
563 options
.installdir
and \
564 not options
.mac_framework
and \
565 not options
.wxpython
: # wxPython's build will do this later if needed
567 prefixDir
= '/usr/local'
568 macFixupInstallNames(options
.installdir
, prefixDir
)#, buildDir)
570 # make a package if a destdir was set.
571 if options
.mac_framework
and \
572 options
.install
and \
573 options
.installdir
and \
576 if os
.path
.exists(options
.mac_distdir
):
577 shutil
.rmtree(options
.mac_distdir
)
579 packagedir
= os
.path
.join(options
.mac_distdir
, "packages")
580 os
.makedirs(packagedir
)
581 basename
= os
.path
.basename(prefixDir
.split(".")[0])
582 packageName
= basename
+ "-" + getWxRelease()
583 packageMakerPath
= "/Developer/usr/bin/packagemaker "
585 args
.append("--root %s" % options
.installdir
)
586 args
.append("--id org.wxwidgets.%s" % basename
.lower())
587 args
.append("--title %s" % packageName
)
588 args
.append("--version %s" % getWxRelease())
589 args
.append("--out %s" % os
.path
.join(packagedir
, packageName
+ ".pkg"))
590 cmd
= packageMakerPath
+ ' '.join(args
)
591 print "cmd = %s" % cmd
594 os
.chdir(options
.mac_distdir
)
596 run('hdiutil create -srcfolder %s -volname "%s" -imagekey zlib-level=9 %s.dmg' % (packagedir
, packageName
, packageName
))
598 shutil
.rmtree(packagedir
)
600 if __name__
== '__main__':
601 exitWithException
= False # use sys.exit instead
602 main(sys
.argv
[0], sys
.argv
[1:])