Writing a python build script for your Visual C++ project

If you’ve decided to write your own python build script for existing Visual Studio projects, then two questions would probably come up at first.
  1. What utility/commands are available for actually building *.sln/*. vcxproj files?
  2. How will you specify the appropriate version for your *.rc version resource file?
Solution for #1. You can download the MSBuild tools for free from the Microsoft download website, and it provides the solution you need. First, download the MS Build tools 2013(or 2015) depending on your Visual Studio project versions.
# Sample code snippet..
def start(self, version):
msbuild = os.getenv('MSBUILD_PATH', r"C:\Program Files\MSBuild\12.0\Bin\MSBuild.exe")
project_output_dir = os.getenv('PROJECT_OUTPUT_DIR', r'c:\Build_distribute\')
if not os.path.exists(msbuild):
raise Exception('not found ' + msbuild)
projects = [r"..\yoursolution.sln", r"..\yourproject\yourproject.vcxproj"]
win32_targets = '/t:ProjectA:rebuild;ProjectB:rebuild;ProjectC:rebuild'
x64_targets = '/t:ProjectA:rebuild;ProjectB:rebuild;ProjectC:rebuild'
rebuild = '/t:Rebuild'
debug = '/p:Configuration=Debug'
release = '/p:Configuration=Release'
x64 = '/p:Platform=x64'
win32 = '/p:Platform=Win32'
xp_toolset = '/p:PlatformToolset=v110/v100/v90'
#msbuild %s.vcxproj /t:rebuild /p:configuration=VC90Release,platform=%s
#msbuild myproject.vcxproj /p:PlatformToolset=v90 /t:rebuild
#msbuild myproject.vcxproj /p:PlatformToolset=v110_xp /t:rebuild
# making command line to run
default = [msbuild]
default.append('/m:1') # https://msdn.microsoft.com/en-us/library/ms164311.aspx
libs = default [:]
libs.append(projects[0]) # append a project/solution name to build command-line
if self.build_arch_target == 'x86':
default.append(win32)
# win32 targets
default.append(win32_targets)
def build(self, lib_to_build):
build_result = False
print ('Build Start ************************')
print ('Target Platform: ' + self.build_arch_target)
print('configuration: ' + self.build_type)
process = subprocess.Popen(args = lib_to_build, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
nextline = process.stdout.readline()
if nextline == b'' and process.poll() != None:
break
sys.stdout.write(nextline.decode('cp949')) # adjust the codepage for your console
sys.stdout.flush()
output = process.communicate()[0]
exitCode = process.returncode
if (exitCode == 0):
build_result = True
pass #return output
else:
build_result = False
#raise Exception(command, exitCode, output)
print ('************************')
print('build finished %d ' % process.returncode)
return build_result

view raw
build.py
hosted with ❤ by GitHub

Solution for #2. You can easily modify/set the version number string on your *.rc resource file by using python regex. Here is the example.
# source taken from
# http://stackoverflow.com/questions/4003725/modifying-rc-file-with-python-regexp-involved
# target_version = "2,3,4,5"
def set_rc_version(rcfile, target_version):
with open(rcfile, "r+") as f:
rc_content = f.read()
# first part
#FILEVERSION 6,0,20,163
#PRODUCTVERSION 6,0,20,163
#…
# second part
#VALUE "FileVersion", "6, 0, 20, 163"
#VALUE "ProductVersion", "6, 0, 20, 163"
# first part
regex_1 = re.compile(r"\b(FILEVERSION|FileVersion|PRODUCTVERSION|ProductVersion) \d+,\d+,\d+,\d+\b", re.MULTILINE)
# second part
regex_2 = re.compile(r"\b(VALUE\s*\"FileVersion\",\s*\"|VALUE\s*\"ProductVersion\",\s*\").*?(\")", re.MULTILINE)
version = r"\1 " + target_version
#modified_str = re.sub(regex, version, rc_content)
pass_1 = re.sub(regex_1, version, rc_content)
version = re.sub(",", ", ", version) #replacing "x,y,v,z" with "x, y, v, z"
pass_2 = re.sub(regex_2, r"\g<1>" + target_version + r"\2", pass_1)
# overwrite
f.seek(0)
f.write(pass_2)
f.truncate()
#set_rc_version(r"C:/repo/projectA/resources/win/projectA.rc", "3,4,5,6")

view raw
manage_version.py
hosted with ❤ by GitHub

One thought on “Writing a python build script for your Visual C++ project

Leave a Reply to Heejune Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s