1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
#!/usr/bin/env python -O
"""
This program takes a Setup file and creates the Makefile-like files
(.dsw and .dsp) needed for simple compilation of Python extension
modules on Windows using DevStudio97.
Author: David Ascher [compile_unix modified from Konrad Hinsen's compile.py]
=======
Features:
=========
- On Unix, does the usual make based on Makefile.pre.in
- On Windows, creates a .DSW file and as many .DSP files as there
are modules in the Setup file
- Should work ok for most Setup files without modifications
- Auto-identifies C and C++ files using wide range of extensions
- Converts -D, -L, -l tags to approprate Windows versions.
- Tries to find the location of python15.lib and the Include
directory (or directories if you have Python before 1.5.1). Asks
the user for their locations if can't find it in a few standard places.
- Builds only one configuration (I *hate* the Debug/Release
user interface.
- Builds a project dependency chain based on the sequential order of
the modules in the Setup file.
- If a .c file ends in _wrap.c, looks for the corresponding .i file in the
same directory. If it finds it, adds the .i file to the project, with
the commands needed to run SWIG (assuming it can find SWIG).
- VC-specific options in a Setup file are indicate with the syntax e.g.
#[VC50]cpp_options.append('/U DOUBLE')
#[VC50]cfiles.append('_numpy.def')
#[VC50]swig_options.append('-shadow')
which can be useful to override defaults set in the Setup file, and
to add files to the compilation which are Windows-only (e.g. DEF files
when building DLLs you want other DLL's to link against).
Default flags are:
Defines: WIN32, NDEBUG, _WINDOWS
Link: wherever the python15.lib is.
Include: wherever the Python includes are.
Useful flags to document in eventual documentation:
/GR : Enable RTTI --- should it be made default if C++?
Usage:
======
On Unix:
csh> compile.py
csh> install.py
On Windows:
c:\wherever> python compile.py workspacename
c:\wherever> msdev workspacename.dsw
[Go to "Build", pull down "Batch Build", do "Build All", exit DevStudio]
# install by hand -- bins are in pyds/, .py's are in lib/
Todo:
=====
* write install.py for Windows
* write test.py for Windows & Unix
* figure out win32 stuff to run the build automagically
* Figure out how to avoid reSWIGGing every time.
Release History:
================
0.21 [Mar 10, 1998] Put back Konrad's new compile stuff.
Should work on Unix as well now [da]
0.2 [Feb 13, 1998] Got rid of unix part -- NumPy specific
Released on newsgroup.
0.1h [Feb 8, 1998] Made options be lists instead of strings (cleaner Setup
files) - backwards compatible thanks to RAddList
0.1g [Feb 8, 1998] fixed bug with -L flag
0.1f [Feb 7, 1998] Added auto-detect and custom build stuff for shadow
files (swig_options, and final 'c' on init routine)
0.1e [Feb 7, 1998] Added auto-detect and custom build stuff for SWIG .i files
0.1d [Feb 6, 1998] Fixed bug re: detection of -l tags.
0.1c [Feb 6, 1998] Limited edition release to P, G, K, H, dek
- 'cfiles' moved into self.__dict__
- fixed Setup file for NumPy to include .def file
0.1b [Feb 6, 1998] Limited edition release to P, G, K, H.
- now putting .lib and .exp with .pyd's
0.1a [Feb 5, 1998] Limited edition release to P
0.0 [Feb 4, 1998] Started work, prompted by discussion with Paul,
Guido, Konrad, Harri.
Known bugs:
===========
* It doesn't work on macs.
* I'd like the .plg files to go in ./tmp (maybe even the .dsp files)
Comments
========
- There is no way, apparently, to specify C++ compilation on a
single file, only on all the files in a module (project) at once.
This is probably not a problem in reality, as most C files go
through C++ compilers.
Bug reports:
============
email me at da@skivs.ski.org
URL: [eventually]
====
http://starship.skyport.net/~da/compile.py/
"""
_version = '0.1h'
import sys, string, os # three oh so cool modules
from UserList import UserList
###
#
# Constants which I guess you can change. Let me know why, and I may
# change the master copy.
#
# These are the extensions of C++ files *other than .cpp and .CPP*
# which should force the use of the C++ compiler. (VC5 recognizes
# .cpp and .CPP automatically).
CPP_EXTENSIONS = ('c++', 'C++', 'C', 'cxx', 'CXX')
# These are lowercased extensions which mark C or C++ files
SOURCE_EXTENSIONS = ('c', 'c++', 'cpp', 'cxx')
# Default define flags. NDEBUG means "Not DEBUG"
DEFAULT_DEFINES = ('WIN32', 'NDEBUG', '_WINDOWS')
# Python's main library [what's the algorithm which determines it from
# sys.version? Will 1.5.1 have a python151.lib? Probably not.]
LIBNAME = "python15.lib"
# Possible places where LIBNAME might be found, if it's not in
# os.path.join(sys.prefix, 'lib') as Guido promised.
LIB_POSSIBLES = (os.path.join(sys.prefix, 'Python-1.5\PCbuild\Release'),
"C:\Python-1.5\PCbuild\Release",
"C:\local\src\Python-1.5\PCbuild\Release",
"D:\Python-1.5\PCbuild\Release",
"D:\local\src\Python-1.5\PCbuild\Release",
)
# Possible places where the Include directory might be, if it's not in
# os.path.join(sys.prefix, 'include') as Guido promised.
# The ../PC directory is inferred from it in this case.
INCLUDE_POSSIBLES = (os.path.join(sys.prefix, 'Python-1.5\Include'),
"C:\Python-1.5\Include",
"C:\local\src\Python-1.5\Include",
"D:\Python-1.5\Include",
"D:\local\src\Python-1.5\Include",
)
# places where we should look for swig.exe
SWIG_POSSIBLES = ('C:\SWIG1.1\SWIG.EXE',
'C:\SWIG1.0\SWIG.EXE',
'D:\SWIG1.1\SWIG.EXE',
'D:\SWIG1.0\SWIG.EXE')
########## end of reasonably configurable things ###############
#
# Note: don't play too much with these -- DevStudio really cares about
# some of the apparently useless things in here (e.g. the BASE
# definitions), and will refuse to deal with "corrupt" project and
# workspace files (and sometimes crash).
#
# If you have suggestions for different compilation options, by all
# means let me know.
#
project_template = r"""# Microsoft Developer Studio Project File - Name="%(projectname)s" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 5.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=%(projectname)s - Win32 Release
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "%(projectname)s.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "%(projectname)s.mak" CFG="%(projectname)s - Win32 Release"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "%(projectname)s - Win32 Release" (based on\
"Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP Scc_ProjName "%(projectname)s"
# PROP Scc_LocalPath ".."
CPP=cl.exe
F90=df.exe
MTL=midl.exe
RSC=rc.exe
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "%(outdir)s"
# PROP Intermediate_Dir "%(tmpdir)s"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE F90 /include:"Release/"
# ADD F90 /include:"%(tmpdir)s/"
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /Ob2 %(includeline)s /YX /FD /c %(cpp_options)s
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib python15.lib /nologo /subsystem:windows /dll /machine:I386 %(linkline)s %(exportline)s %(outline)s
# Begin Target
# Name "%(projectname)s - Win32 Release"
%(sourcesection)s
# End Target
# End Project
"""
# template for C files
cfiletemplate = r"""# Begin Source File
SOURCE=%(sourcepath)s
# End Source File
"""
# Template for SWIG files
ifiletemplate = r"""# Begin Source File
SOURCE=./%(sourcepath)s
# PROP Ignore_Default_Tool 1
# Begin Custom Build - SWIG'ing
InputPath=./%(sourcepath)s
InputName=%(prefix)s
"$(InputName)_wrap.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
c:\swig1.1\swig -python -Iinclude -I. %(swig_options)s $(InputPath)
# End Custom Build
# End Source File
"""
projectInWorkspaceTemplate = r"""Project: "%(projectname)s"="%(projectfilepath)s" - Package Owner=<4>
Package=<5>
{{{
begin source code control
%(projectname)s
%(thedir)s
end source code control
}}}
Package=<4>
{{{
%(dependency_part)s
}}}
"""
workspacetemplate = r"""Microsoft Developer Studio Workspace File, Format Version 5.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
%(projectinfo)s
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################
"""
# These parse the 'words' in the Setup lines
# Get source files
def get_cfiles(candidates):
cfiles = []
rest = []
for candidate in candidates:
if string.find(candidate, '.') != -1:
if string.lower(string.split(candidate, '.')[-1]) in SOURCE_EXTENSIONS:
if string.find(candidate, '/'):
candidate = string.replace(candidate, '/', '\\')
cfiles.append(candidate)
else:
rest.append(candidate)
else:
rest.append(candidate)
return cfiles, rest
# helper function
def get_prefixed(candidates, *prefixlist):
matched = []
rest = []
for candidate in candidates:
for prefix in prefixlist:
if string.find(candidate, prefix) == 0:
matched.append(os.path.normpath(candidate[len(prefix):]))
else:
rest.append(candidate)
return matched, rest
def get_includedirs(candidates):
return get_prefixed(candidates, '-I')
def get_libfiles(candidates):
return get_prefixed(candidates, '-l')
def get_libdirs(candidates):
return get_prefixed(candidates, '-L')
# look in the someday standard place for e.g. lib and include directories.
def get_pydir(what):
attempt = os.path.join(sys.prefix, what)
if os.path.exists(attempt):
return attempt
# find the Python15.lib library
def get_pylibsdir():
attempt = get_pydir('libs')
if attempt:
if os.path.exists(os.path.join(attempt, LIBNAME)):
if __debug__: print " Found " +LIBNAME+" in", attempt
return [attempt]
attempt = None
if not attempt:
for attempt in LIB_POSSIBLES:
if os.path.exists(os.path.join(attempt, LIBNAME)):
if __debug__: print " Found " + LIBNAME + " in", attempt
return [attempt]
while 1:
attempt = raw_input("What directory is python15.lib in? ")
if os.path.exists(os.path.join(attempt, LIBNAME)):
if __debug__: print " Found " + LIBNAME + " in", attempt
return [attempt]
# checks the potentially singleton list of Python include directories
def valid_includedir(attempt):
if os.path.exists(attempt):
attempt = [os.path.join(os.path.split(attempt)[0], 'PC'), attempt]
if not os.path.exists(attempt[0]):
attempt = [attempt[1]] # try with just main one.
if __debug__: print " Found Python include directories in", string.join(attempt, ', ')
return attempt
return None
# find the list of Python include directories
def get_pyincludedir():
attempt = get_pydir('include')
if attempt:
if __debug__: print " Found Python include directory in", attempt
return [attempt]
if not attempt:
for attempt in INCLUDE_POSSIBLES:
gooddirs = valid_includedir(attempt)
if gooddirs:
return gooddirs
while 1:
attempt = raw_input("Where is the Python include directory? ")
gooddirs = valid_includedir(attempt)
if gooddirs:
return gooddirs
# The following few functions return things which can be understood by
# the VC and LINK executables
# helper function
def make_mssyntax(libdirs, sep):
def quote(x):
return '"'+x+'"'
return sep + string.join(map(quote,libdirs), sep)
# equivalent of -L
def make_libdirline(libdirs):
return make_mssyntax(libdirs, ' /libpath:')
#equivalent of -l
def make_libfileline(libdirs):
return string.join(map(lambda x: x + '.lib ', libdirs))
#equivalent of -D
def make_defline(defines):
return make_mssyntax(defines, ' /D ')
#equivalent of -I
def make_includeline(includepaths):
return make_mssyntax(includepaths, ' /I ')
def search_for_ifiles(cfiles):
wrapfiles = filter(lambda x: x[-7:] == '_wrap.c', cfiles)
ifiles = map(lambda x: x[:-7]+'.i', wrapfiles)
ifiles = filter(lambda x: os.path.exists(x), ifiles)
return ifiles
def find_swig():
for candidate in SWIG_POSSIBLES:
if os.path.exists(candidate):
return candidate
# not found
return None
class RAddList(UserList):
""" a list which allows addition with strings -- for backwards
compatibility with all 2 users =)"""
def __init__(self, data):
UserList.__init__(self, data)
def __add__(self, what):
self.append(what)
return self
class Project:
"""
A Project corresponds to a single (logical) line in a Setup file
"""
def __init__(self, ws, line, extra_stuff):
try:
words = string.split(line)
self.projectname, rest = words[0], words[1:]
except TypeError:
raise "Couldn't parse the Setup file!"
print "Building project (.dsp) file for", self.projectname
# parse the words in the line
self.cfiles, rest = get_cfiles(rest)
incstmts, rest = get_includedirs(rest)
libfilestmts, rest = get_libfiles(rest)
libdirstmts, rest = get_libdirs(rest)
# search for SWIG output file, and return the corresponding .i files which exist
if swig_found:
self.ifiles = search_for_ifiles(self.cfiles)
else:
self.ifiles = []
libdiritems = ws.libsdir
libfileitems = []
includepaths = ws.incsdir
for libdir in libdirstmts:
if libdir not in libdiritems:
libdiritems.append(libdir)
for libfile in libfilestmts:
if libfile not in libfileitems:
libfileitems.append(libfile)
for incstmt in incstmts:
if incstmt not in includepaths:
includepaths.append(incstmt)
self.defines = DEFAULT_DEFINES
self.linkline = string.join(string.split(make_libdirline(libdiritems) + ' ' + make_libfileline(libfileitems)))
self.includeline = make_includeline(includepaths)
self.defline = make_defline(self.defines)
# the following detects C++ files which VC5 won't recognize as C++
# files, and forces VC to compile them as C++ files
def iscppfile(fname):
extensions = CPP_EXTENSIONS # extend as needed
ext = string.split(fname, '.')[-1]
return ext in extensions
# if any of the files for this module are C++ files..
if filter(iscppfile, self.cfiles):
self.cpp_options = RAddList(["/Tp"]) # force C++ compilation of all of them
self.swig_options = RAddList(['-c++'])
else:
self.cpp_options = RAddList([])
self.swig_options = RAddList([])
# if there was any Setup-specified code, execute it now
if extra_stuff:
exec extra_stuff+'\n\n\n' in self.__dict__
self.cpp_options = self.defline + ' ' + string.join(self.cpp_options, ' ')
# The init function of the module needs to be exported
# (replaces use of .DEF file)
building_shadow_module = '-shadow' in self.swig_options
if building_shadow_module:
self.exportline = "/export:init"+self.projectname+'c'
self.outline = "/out:pyds/"+self.projectname+'c.pyd'
else:
self.exportline = "/export:init"+self.projectname
self.outline = "/out:pyds/"+self.projectname+'.pyd'
# make target directory if needed
if not os.path.exists('pyds'):
os.mkdir('pyds')
self.outdir = "pyds"
self.tmpdir = "tmp"
self.sourcesection = ""
self.swig_options = string.join(self.swig_options, ' ')
for self.sourcepath in self.cfiles:
# The variables in the template are all instance
# attributes of our self.
self.sourcesection = self.sourcesection + cfiletemplate % self.__dict__
for self.sourcepath in self.ifiles:
# The variables in the template are all instance
# attributes of our self.
self.prefix = os.path.split(self.sourcepath)[-1][:-2]
self.sourcesection = self.sourcesection + ifiletemplate % self.__dict__
project_file_text = project_template % self.__dict__
project_file = open(self.projectname+'.dsp', 'w')
project_file.write(project_file_text)
class Workspace:
"""
A Workspace is needed for the entire workspace.
"""
def __init__(self, setupfilename = 'Setup', workspacename = 'workspace'):
self.workspacename = workspacename
print "Building workspace (.dsw) file for", self.workspacename
# read the setup file
lines = open(setupfilename, 'r').readlines()
# filter the comments out (watching out for the VC50 commands)
lines = filter(lambda line: line[0] != '#' or line[:7] == '#[VC50]', lines)
# filter the extra whitespace
lines = map(string.rstrip, lines)
# filter out the blank lines
lines = filter(None, lines)
# filter out the *setup* lines
lines = filter(lambda line: line != '*shared*', lines)
lines = filter(lambda line: line != '*noconfig*', lines)
# join the lines which need joining
newlines = [lines[0]]
lastline = lines[0]
for line in lines[1:]:
if lastline[-1] == '\\':
newlines[:-1] = newlines[:-1] + line
else:
newlines.append(line)
lastline = line
self.projects = []
# get value for location of python15.lib
self.libsdir = get_pylibsdir()
# get value for location of Python's include directory(ies)
self.incsdir = get_pyincludedir()
# since #[VC50] things apply to the *next* module, keep track
# of them.
extra_stuff = ''
for line in lines:
if line[:7] == '#[VC50]':
extra_stuff = extra_stuff + '\n'+ line[7:]
else:
# create project file
project = Project(self, line, extra_stuff)
self.projects.append(project)
extra_stuff = '' # new project does not inherit last
# one's flags etc.
self.output_workspace_file()
def output_workspace_file(self):
# the only tricky thing here is the fact that each project
# depends (in the sense of a Makefile dependency) on the last
# one), so that e.g. the modules in NumPy can be built after
# _numpy has been built (otherwise DevStudio goes in
# *alphabetical* order!).
projectinfo = ""
oldp = None
for project in self.projects:
dependency_part = ""
projectname = project.projectname
projectfilepath = projectname+".dsp"
# making thedir be this value prevents the creation of a random
# directory (see template for project file).
# [it'd be nice to understand exactly what this is about
thedir = os.path.join(os.pardir, os.path.split(os.getcwd())[1])
if oldp:
dependency_part = 'Begin Project Dependency\nProject_Dep_Name '+oldp.projectname+'\nEnd Project Dependency\n'
else:
dependency_part = ''
project_file_text = projectInWorkspaceTemplate % locals()
projectinfo = projectinfo + project_file_text + '\n'
oldp = project
workspace_text = workspacetemplate % locals()
workspace_file = open(self.workspacename+'.dsw', 'w')
workspace_file.write(workspace_text)
def compile_unix():
import os, sys
import compileall
lib = os.path.join(os.path.join(sys.exec_prefix, 'lib'),
'python'+sys.version[:3])
if not os.path.exists('Makefile.pre.in'):
source = os.path.join(os.path.join(lib, 'config'), 'Makefile.pre.in')
if os.path.exists(source):
os.system('cp ' + source + ' .')
else:
print "Copy Misc/Makefile.pre.in from the Python distribution"
print "to this directory and try again."
compileall.compile_dir(".")
os.system("make -f Makefile.pre.in boot")
os.system("make")
if sys.platform == 'win32':
swig_found = find_swig()
ws = Workspace()
else:
compile_unix()
|