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.
- What utility/commands are available for actually building *.sln/*. vcxproj files?
- 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.
- Microsoft Build Tools 2015 https://www.microsoft.com/en-us/download/details.aspx?id=48159
- Microsoft Build Tools 2013 https://www.microsoft.com/en-us/download/details.aspx?id=40760
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
for C# project, here https://wordpress.com/post/heejune.me/528