60 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
 | 
						|
import os
 | 
						|
import pathlib
 | 
						|
import subprocess
 | 
						|
import sys
 | 
						|
from SCons.Script import *
 | 
						|
 | 
						|
_BUILT_STAMPFILE = '.spp_built'
 | 
						|
 | 
						|
Import('env')
 | 
						|
 | 
						|
def _autotools_project(env: Environment, project_root: str, config_args: 'list[str]' = [], build_args : 'list[str]' = [], install_args : 'list[str]' = [], configure_script_path: str = 'configure', skip_steps = ()) -> dict:
 | 
						|
    config = env['BUILD_TYPE']
 | 
						|
    build_dir = os.path.join(project_root, f'build_{config}')
 | 
						|
    install_dir = os.path.join(project_root, f'install_{config}')
 | 
						|
    is_built = os.path.exists(os.path.join(install_dir, _BUILT_STAMPFILE))
 | 
						|
    if not is_built or env['UPDATE_REPOSITORIES']:
 | 
						|
        print(f'Building {project_root}, config {config}')
 | 
						|
        os.makedirs(build_dir, exist_ok=True)
 | 
						|
        opt_level = {
 | 
						|
            'debug': '-O0',
 | 
						|
        }.get(env['BUILD_TYPE'], '-O2')
 | 
						|
        debug_symbols = {
 | 
						|
            'release': ''
 | 
						|
        }.get(env['BUILD_TYPE'], '-g')
 | 
						|
        cflags = f'{opt_level} {debug_symbols}'
 | 
						|
        jobs = env.GetOption('num_jobs')
 | 
						|
        env = os.environ.copy()
 | 
						|
        env['CFLAGS'] = cflags
 | 
						|
        
 | 
						|
        config_script = os.path.join(project_root, configure_script_path)
 | 
						|
        if not os.path.exists(config_script) and os.path.exists(f'{config_script}.ac'):
 | 
						|
            subprocess.run(('autoreconf', '--install', '--force'), cwd=project_root)
 | 
						|
 | 
						|
        if 'configure' not in skip_steps:
 | 
						|
            subprocess.run((config_script, f'--prefix={install_dir}', *config_args), cwd=build_dir, env=env, stdout=sys.stdout, stderr=sys.stderr, check=True)
 | 
						|
        if 'build' not in skip_steps:
 | 
						|
            subprocess.run(('make', f'-j{jobs}', *build_args), cwd=build_dir, stdout=sys.stdout, stderr=sys.stderr, check=True)
 | 
						|
        if 'install' not in skip_steps:
 | 
						|
            subprocess.run(('make', 'install', *install_args), cwd=build_dir, stdout=sys.stdout, stderr=sys.stderr, check=True)
 | 
						|
        else:
 | 
						|
            # must still create the install dir for the stamp file
 | 
						|
            os.makedirs(install_dir, exist_ok=True)
 | 
						|
        pathlib.Path(install_dir, _BUILT_STAMPFILE).touch()
 | 
						|
 | 
						|
    libpath = []
 | 
						|
    for lib_folder in ('lib', 'lib64'):
 | 
						|
        full_path = os.path.join(install_dir, lib_folder)
 | 
						|
        if os.path.exists(full_path):
 | 
						|
            libpath.append(full_path)
 | 
						|
 | 
						|
    return {
 | 
						|
        'build_dir': build_dir,
 | 
						|
        'install_dir': install_dir,
 | 
						|
        'LIBPATH': libpath,
 | 
						|
        'CPPPATH': [os.path.join(install_dir, 'include')]
 | 
						|
    }
 | 
						|
 | 
						|
env.AddMethod(_autotools_project, 'AutotoolsProject')
 | 
						|
Return('env') |