Skip to content

Instantly share code, notes, and snippets.

@leeonix
Last active March 8, 2017 01:33
Show Gist options
  • Save leeonix/4155c44597e3c211d2d8 to your computer and use it in GitHub Desktop.
Save leeonix/4155c44597e3c211d2d8 to your computer and use it in GitHub Desktop.
premake5.lua or premake4.lua generator
import os, sys, getopt, string
import uuid
template4 = """
newoption {
trigger = 'to',
value = 'path',
description = 'Set the output location for the generated files'
}
solution '${name}'
configurations { 'Debug', 'Release' }
language '${language}'
defines { 'WIN32', '_WINDOWS' }
flags { 'StaticRuntime' }
location (_OPTIONS['to'] or 'build')
targetdir 'bin'
targetname '${name}'
objdir 'obj'
configuration 'vs*'
defines { '_CRT_SECURE_NO_WARNINGS' }
-- buildoptions '/FI"pch.h"'
-- pchheader 'pch.h'
-- pchsource 'src/pch.c'
configuration 'Debug'
defines { '_DEBUG' }
flags { 'Symbols' }
targetsuffix '_d'
configuration 'Release'
defines { 'NDEBUG' }
flags { 'Optimize' }
project '${name}'
uuid '${uuid}'
kind '${kind}'
-- flags { 'WinMain' }
includedirs {
}
libdirs {
}
links {
}
files {
'*.h',
'*.c',
'*.cpp',
}
"""
template5 = """
newoption {
trigger = 'to',
value = 'path',
default = 'build',
description = 'Set the output location for the generated files'
}
workspace '${name}'
configurations { 'Debug', 'Release' }
platforms { 'Win32', 'Win64' }
language '${language}'
defines { 'WIN32', '_WINDOWS' }
flags { 'StaticRuntime' }
characterset ("MBCS")
location(_OPTIONS['to'])
targetdir 'bin'
targetname '${name}'
objdir 'obj'
filter { "platforms:Win32" }
system "Windows"
architecture "x32"
filter { "platforms:Win64" }
system "Windows"
architecture "x64"
filter 'action:vs*'
disablewarnings {
'4996', -- 4996 - same as define _CRT_SECURE_NO_WARNINGS
}
-- forceincludes 'pch.h'
-- pchheader 'pch.h'
-- pchsource 'src/pch.c'
filter 'configurations:Debug'
defines { '_DEBUG' }
symbols 'On'
symbolspath '$(OutDir)$(TargetName).pdb'
optimize 'Debug'
targetsuffix '_d'
filter { 'configurations:Debug', 'action:vs2015 or vs2017' }
-- vs2015 and later version StaticRuntime will link libvcruntimed and libucrtd
links {
'libvcruntimed',
'libucrtd',
}
-- vs2015 DLLRuntime will add vcruntimed and ucrtd
-- links {
-- 'vcruntimed',
-- 'ucrtd',
-- }
filter 'configurations:Release'
defines { 'NDEBUG' }
optimize 'Full'
project '${name}'
uuid '${uuid}'
kind '${kind}'
-- flags { 'WinMain' }
includedirs {
}
libdirs {
}
links {
}
files {
'*.h',
'*.c',
'*.cpp',
}
"""
kind_table = {
'con' : 'ConsoleApp',
'win' : 'WindowedApp',
'dll' : 'SharedLib',
'lib' : 'StaticLib',
}
Usage = """
Usage:
python %s -n(name) -l(language) -k(kind) -v(Premake4 or Premake5)
"""
Help = """
Usage python %s [options]
-h, --help Display this infomation.
-n, --name solution and project name. it's required
-l, --language Premake can use programming language. it's optional default is C++.
-k, --kind Output kind, use con win dll lib for Premake parameter
ConsoleApp WindowApp SharedLib StaticLib. it's optional default is con.
-v, --version Generate Premake file's version. it's optional default is 5.
"""
def write_file(ver):
f = open('premake' + str(ver) + '.lua', 'w')
if ver == 5:
f.write(string.Template(template5).safe_substitute(content))
elif ver == 4:
f.write(string.Template(template4).safe_substitute(content))
f.close()
def main():
global content
content = {}
ver = 5
try:
optlist, args = getopt.getopt(sys.argv[1:], 'n:l:k:v:h', ['name=', 'language=', 'kind=', 'version=', 'help'])
except getopt.GetoptError, msg:
sys.stderr.write("which: error: %s. Your invocation was: %s\n" % (msg, argv))
return 1
for opt, optarg in optlist:
if optarg == '':
continue
if opt in ('-n', '--name'):
content['name'] = optarg
elif opt in ('-l', '--language'):
content['language'] = optarg.upper()
elif opt in ('-k', '--kind'):
content['kind'] = kind_table[optarg.lower()]
elif opt in ('-v', '--version'):
ver = int(optarg)
elif opt in('-h', '--help'):
sys.stderr.write(Help % sys.argv[0])
sys.exit(1)
if not content.has_key('name') or content['name'] == '':
sys.stderr.write(Usage % sys.argv[0])
return 1
if not content.has_key('language') or content['language'] == '':
content['language'] = 'C++'
if not content.has_key('kind') or content['kind'] == '':
content['kind'] = 'ConsoleApp'
content['uuid'] = str(uuid.uuid4()).upper()
write_file(ver)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment