pocketlang/build/SConstruct
2021-06-09 16:12:26 +05:30

185 lines
6.1 KiB
Python

#!python
## Copyright (c) 2020-2021 Thakee Nathees
## Distributed Under The MIT License
from os.path import join
import os, sys
## -----------------------------------------------------------------------------
## PROJECT CONFIGURATIONS
## -----------------------------------------------------------------------------
PROJECT_NAME = 'pocketlang'
def CONFIGURE_ENV(env):
variant_dir = env['variant_dir']
binary_dir = join(variant_dir, 'bin/')
binary_name = 'pocket'
## The executable to run with the current configuration.
env.RUN_TARGET = join(variant_dir, 'bin', binary_name)
env.SOURCE_DIRS = ('../src/', '../src/include/', '../cli/', '../cli/modules/')
## PocketLang source files
PK_SOURCES = Glob(join(variant_dir, 'src/*.c'))
if env['shared_lib']:
## Compile pocketlang dynamic lib.
dll = env.SharedLibrary(
target = join(binary_dir),
source = PK_SOURCES,
CPPDEFINES = [env['CPPDEFINES'], 'PK_DLL', 'PK_COMPILE'],
)
else:
## Compile pocketlang static lib.
lib = env.Library(
target = join(binary_dir, binary_name),
source = PK_SOURCES,
)
## Test executable
test = env.Program(
target = join(binary_dir, binary_name),
source = Glob(join(variant_dir, 'cli/*.c')),
CPPPATH = [ join(variant_dir, 'src/include/') ],
LIBPATH = binary_dir,
LIBS = binary_name,
)
Requires(test, lib)
## Adding CPPPATH here to make VS be able to find the included files.
env.Append(CPPPATH=['../src/include/'])
## -----------------------------------------------------------------------------
## COMMAND LINE ARGUMENTS
## -----------------------------------------------------------------------------
opts = Variables([], ARGUMENTS)
## Define our options.
opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'x11', 'linux', 'osx']))
opts.Add(EnumVariable('target', "Compilation target", 'debug', ['debug', 'release']))
opts.Add(EnumVariable('bits', "output program bits", '64', ['32', '64']))
opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", False))
opts.Add(BoolVariable('use_mingw', "Use Mingw compiler", False))
opts.Add(BoolVariable('vsproj', "make a visual studio project", False))
opts.Add(BoolVariable('shared_lib', "Compile as a shared library (only).", False))
## -----------------------------------------------------------------------------
## SETUP ENVIRONMENT
## -----------------------------------------------------------------------------
## VariantDir.
variant_dir = ARGUMENTS.get('target', 'debug') + '/'
VariantDir(variant_dir, '../', duplicate=0)
## Setup the Environment.
DefaultEnvironment(tools=[]) ## Not using any tools.
env = Environment()
## Updates the environment with the option variables.
opts.Update(env)
if env['use_llvm']:
env['CC'] = 'clang'
env['CXX'] = 'clang++'
elif env['use_mingw']:
env['tools'] = ['mingw']
## Find platform.
if env['platform'] == '':
if sys.platform == 'win32':
env['platform'] = 'windows'
elif sys.platform in ('x11', 'linux', 'linux2'):
env['platform'] = 'linux'
elif sys.platform == 'darwin':
env['platform'] = 'osx'
else:
print("platform(%s) not supported." % sys.platform)
quit()
if env['target'] == 'debug':
env.Append(CPPDEFINES=['DEBUG'])
## Check our platform specifics
if env['platform'] == "osx":
if env['target'] == 'debug':
env.Append(CCFLAGS=['-g', '-O2', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
else:
env.Append(CCFLAGS=['-g', '-O3', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
elif env['platform'] == 'x11':
env.Append(LIBS=['m'])
if env['target'] == 'debug':
env.Append(CCFLAGS=['-fPIC', '-g3', '-Og'])
else:
env.Append(CCFLAGS=['-fPIC', '-g', '-O3'])
elif env['platform'] == "windows":
env.Append(CPPDEFINES=['_CRT_SECURE_NO_WARNINGS'])
env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS'])
env.Append(CCFLAGS=['-W3', '-GR', '/FS'])
env.Append(LINKFLAGS='-SUBSYSTEM:CONSOLE')
env.Append(LIBS=[])
if env['bits'] == '32': env['TARGET_ARCH'] = 'x86'
else: env['TARGET_ARCH'] = 'x86_64'
if env['target'] == 'debug':
env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
env.Append(LINKFLAGS=['-DEBUG'])
else:
env.Append(CPPDEFINES=['NDEBUG'])
env.Append(CCFLAGS=['-O2', '-EHsc', '-MD'])
## -----------------------------------------------------------------------------
## UPDATE ENV WITH PROJECT CONFIG
## -----------------------------------------------------------------------------
env['variant_dir'] = variant_dir
CONFIGURE_ENV(env)
## -----------------------------------------------------------------------------
## MSVS PROJECT FILE GENERATION
## -----------------------------------------------------------------------------
def msvc_build_commandline(commands):
common_build_prefix = [
'cmd /V /C set "bits=64"',
'(if "$(PlatformTarget)"=="x86" (set "bits=32"))']
return " ^& ".join(common_build_prefix + [commands])
def collect_source_files(dirs, ext):
ret = []
for dir in dirs:
ret += [
join(dir, file) for file in os.listdir(dir) if file.endswith(ext)
]
return ret
if env['vsproj']:
env["MSVSBUILDCOM"] = msvc_build_commandline(
"scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" platform=windows target=$(Configuration) bits=!bits!")
env["MSVSREBUILDCOM"] = msvc_build_commandline(
"scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" platform=windows target=$(Configuration) bits=!bits! vsproj=yes")
env["MSVSCLEANCOM"] = msvc_build_commandline(
"scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" --clean platform=windows bits=!bits! target=$(Configuration)")
targets = [ env.RUN_TARGET ] * 4
variants = ["debug|Win32", "debug|x64", "release|Win32", "release|x64"]
env.MSVSProject(
target = PROJECT_NAME + env['MSVSPROJECTSUFFIX'],
srcs = collect_source_files(env.SOURCE_DIRS, ('.c', '.cpp', '.cc', '.cxx')),
incs = collect_source_files(env.SOURCE_DIRS, ('.h', '.hpp')),
variant = variants,
runfile = targets,
buildtarget = targets,
)
## Generates help for the -h scons option.
Help(opts.GenerateHelpText(env))