Last active
March 10, 2023 08:52
-
-
Save Ohjurot/9df9482a06e6ed15b4b2d6428e314fe8 to your computer and use it in GitHub Desktop.
Python script that will call conan twice (Debug and Release) and generate a premake5 lua file to use both configuration simultaniously
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
""" | |
Simple python script for using conan Debug and Release configurations | |
simultaniously. Take the normal (non generator) create conanbuildinfo.txt | |
and converts it to a proper premake5 lua file. Invokes conan twice with | |
build_type=Debug and build_type=Release | |
!IMPORTANT! | |
This Script is not compatible with conan >= 2.0.0 | |
If you are looking for a conan >= 2.0.0 solution for premake5, consider | |
this PR: https://github.com/conan-io/conan/pull/13390 | |
(c) Copyright 2023 by Ludwig Fuechsl | |
Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
Revision: 3 (10.03.2023) | |
Revisions: | |
1) - Initial draft | |
2) - Added support for emitting lua strings that contain quotation marks | |
- The tool now outputs all variables/output from conan as lua arrays | |
3) - Fixed more lua invalid chars (both in vars and strings) | |
""" | |
import subprocess | |
import os.path | |
import json | |
import sys | |
# Replacement vectors | |
LUA_STR_CHARMAP = { | |
'\\' : '\\\\', | |
'"' : '\\"', | |
} | |
LUA_VAR_CHARMAP = { | |
'-' : '_', | |
'+' : 'p', | |
} | |
def StringApplyReplacementVector(str, vector): | |
for search, replace in vector.items(): | |
str = str.replace(search, replace) | |
return str | |
def RunConanCommand(configuration, args): | |
subprocess.run(("conan", "install", ".", *args, "-s", f"build_type={configuration}")) | |
def JSONFromConanbuildinfo(configuration): | |
with open("./conanbuildinfo.txt") as conanFile: | |
conanCommands = [l for l in [l.strip() for l in conanFile] if len(l) > 0] | |
# Create file if not existing | |
if not os.path.exists("./conanbuildinfo.json"): | |
with open("./conanbuildinfo.json", "w") as jsonFile: | |
jsonFile.write("{}") | |
# Read json | |
with open("./conanbuildinfo.json", "r+") as jsonFile: | |
buildInfo = json.load(jsonFile) | |
# Get Release / Debug section | |
if configuration not in buildInfo: | |
buildInfo[configuration] = {} | |
configurationBuildInfo = buildInfo[configuration] | |
# Modify json | |
jctx = None | |
for cmd in conanCommands: | |
if cmd[0] == "[": | |
keyName = cmd[1:-1] | |
if keyName in configurationBuildInfo: | |
configurationBuildInfo.pop(keyName, None) | |
configurationBuildInfo[keyName] = [] | |
jctx = configurationBuildInfo[keyName] | |
else: | |
if jctx != None: | |
jctx.append(cmd) | |
# Save json | |
jsonFile.truncate(0) | |
jsonFile.seek(0) | |
json.dump(buildInfo, jsonFile, indent=4) | |
def LUAEscapeString(str): | |
return StringApplyReplacementVector(str, LUA_STR_CHARMAP) | |
def LUAReplaceVarname(var): | |
return StringApplyReplacementVector(var, LUA_VAR_CHARMAP) | |
def LUAEmmitArray(f, name, data): | |
luaString = ", ".join(['"' + LUAEscapeString(l) + '"' for l in data]) | |
f.write(f"{LUAReplaceVarname(name)} = {'{' + luaString + '}'}\n") | |
def LUAPrintConfiguration(f, configuration, data): | |
for k, v in data.items(): | |
LUAEmmitArray(f, f"{configuration}_conan_{k}", v) | |
def LUAFromJSON(): | |
# Open json | |
with open("./conanbuildinfo.json", "r") as jsonFile: | |
buildInfo = json.load(jsonFile) | |
with open("./conanbuildinfo.lua", "w") as luaFile: | |
luaFile.truncate(0) | |
LUAPrintConfiguration(luaFile, "debug", buildInfo["Debug"]) | |
LUAPrintConfiguration(luaFile, "release", buildInfo["Release"]) | |
def Generate(conan_args=()): | |
RunConanCommand("Debug", conan_args) | |
JSONFromConanbuildinfo("Debug") | |
RunConanCommand("Release", conan_args) | |
JSONFromConanbuildinfo("Release") | |
LUAFromJSON() | |
if __name__ == "__main__": | |
args = sys.argv[1:] | |
Generate(conan_args=args) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment