From c29ad6b598557d3bebe3fb1cf29041c29cdae20f Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 21 Feb 2024 02:57:07 +0300 Subject: [PATCH] scripts: waifulib: compiler_optimizations: add option to use profiling --- scripts/waifulib/compiler_optimizations.py | 31 ++++++++ scripts/waifulib/psp.py | 82 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 scripts/waifulib/psp.py diff --git a/scripts/waifulib/compiler_optimizations.py b/scripts/waifulib/compiler_optimizations.py index f65e7aeb..30ab6baf 100644 --- a/scripts/waifulib/compiler_optimizations.py +++ b/scripts/waifulib/compiler_optimizations.py @@ -116,6 +116,22 @@ POLLY_CFLAGS = { # msvc sosat :( } +PROFILE_GENERATE_CFLAGS = { + 'gcc': ['-fprofile-generate=xash3d-prof'], +} + +PROFILE_GENERATE_LINKFLAGS = { + 'gcc': ['-fprofile-generate=xash3d-prof'], +} + +PROFILE_USE_CFLAGS = { + 'gcc': ['-fprofile-use=%s'], +} + +PROFILE_USE_LINKFLAGS = { + 'gcc': ['-fprofile-use=%s'], +} + def options(opt): grp = opt.add_option_group('Compiler optimization options') @@ -128,6 +144,12 @@ def options(opt): grp.add_option('--enable-poly-opt', action = 'store_true', dest = 'POLLY', default = False, help = 'enable polyhedral optimization if possible [default: %default]') + grp.add_option('--enable-profile', action = 'store_true', dest = 'PROFILE_GENERATE', default = False, + help = 'enable profile generating build (stored in xash3d-prof directory) [default: %default]') + + grp.add_option('--use-profile', action = 'store', dest = 'PROFILE_USE', default = None, + help = 'use profile during build [default: %default]') + def configure(conf): conf.start_msg('Build type') @@ -144,6 +166,8 @@ def configure(conf): conf.msg('LTO build', 'yes' if conf.options.LTO else 'no') conf.msg('PolyOpt build', 'yes' if conf.options.POLLY else 'no') + conf.msg('Generate profile', 'yes' if conf.options.PROFILE_GENERATE else 'no') + conf.msg('Use profile', conf.options.PROFILE_USE if not conf.options.PROFILE_GENERATE else 'no') # -march=native should not be used if conf.options.BUILD_TYPE.startswith('fast'): @@ -174,6 +198,13 @@ def get_optimization_flags(conf): if conf.options.POLLY: cflags += conf.get_flags_by_compiler(POLLY_CFLAGS, conf.env.COMPILER_CC) + if conf.options.PROFILE_GENERATE: + linkflags+= conf.get_flags_by_compiler(PROFILE_GENERATE_LINKFLAGS, conf.env.COMPILER_CC) + cflags += conf.get_flags_by_compiler(PROFILE_GENERATE_CFLAGS, conf.env.COMPILER_CC) + elif conf.options.PROFILE_USE: + linkflags+= [conf.get_flags_by_compiler(PROFILE_USE_LINKFLAGS, conf.env.COMPILER_CC)[0] % conf.options.PROFILE_USE] + cflags += [conf.get_flags_by_compiler(PROFILE_USE_CFLAGS, conf.env.COMPILER_CC)[0] % conf.options.PROFILE_USE] + if conf.env.DEST_OS == 'nswitch' and conf.options.BUILD_TYPE == 'debug': # enable remote debugger cflags.append('-DNSWITCH_DEBUG') diff --git a/scripts/waifulib/psp.py b/scripts/waifulib/psp.py new file mode 100644 index 00000000..74f4f953 --- /dev/null +++ b/scripts/waifulib/psp.py @@ -0,0 +1,82 @@ +# encoding: utf-8 +# psp.py -- PSP EBOOT task +# Copyright (C) 2023 Sergey Galushko +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +################################## +# PSP Tools # +################################## +class psp_fixup(Task.Task): + run_str = '${FIXUP} -o ${TGT} ${SRC}' + color = 'BLUE' + +class psp_prxgen(Task.Task): + run_str = '${PRXGEN} ${SRC} ${TGT}' + color = 'BLUE' + +class psp_strip(Task.Task): + run_str = '${STRIP} -o ${TGT} ${SRC}' + color = 'BLUE' + +class psp_mksfo(Task.Task): + run_str = '${MKSFO} -d MEMSIZE=1 ${PSP_EBOOT_TITLE} ${TGT}' + color = 'YELLOW' + +class psp_packpbp(Task.Task): + run_str = '${PACK_PBP} ${TGT} ${SRC[1].abspath()} ${PSP_EBOOT_ICON} ${PSP_EBOOT_ICON1} ${PSP_EBOOT_UNKPNG} ${PSP_EBOOT_PIC1} ${PSP_EBOOT_SND0} ${SRC[0].abspath()} ${PSP_EBOOT_PSAR}' + color = 'GREEN' + + +@TaskGen.feature('cshlib', 'cxxshlib') +@TaskGen.after_method('apply_link') +def build_module(self): + link_output = self.link_task.outputs[0] + for d in self.env.STATIC_LINKING: + if link_output.name.startswith(d): + return + fixup_output = self.path.find_or_declare(link_output.name + '_fixup') + prxgen_output = self.path.find_or_declare(link_output.change_ext('.prx').name) + + task = self.create_task('psp_fixup', src=link_output, tgt=fixup_output) + task = self.create_task('psp_prxgen', src=fixup_output, tgt=prxgen_output) + + if getattr(self, 'install_path', None): + if self.bld.is_install: + for k in self.install_task.inputs: + if k == self.path.find_or_declare(link_output.name): + self.install_task.inputs.remove(k) + self.add_install_files(install_to=self.install_path, install_from=prxgen_output) + +@TaskGen.feature('cprogram', 'cxxprogram', 'cprogram_static', 'cxxprogram_static') +@TaskGen.after_method('apply_link') +def build_eboot(self): + finalobj_ext = '.elf' + finalobj_tool = 'psp_strip' + if self.env.PSP_BUILD_PRX: + finalobj_ext = '.prx' + finalobj_tool = 'psp_prxgen' + + link_output = self.link_task.outputs[0] + fixup_output = self.path.find_or_declare(link_output.name + '_fixup') + finalobj_output = self.path.find_or_declare(link_output.change_ext(finalobj_ext).name) + + mksfo_output = self.path.find_or_declare('PARAM.SFO') + packpbp_output = self.path.find_or_declare('EBOOT.PBP') + + task = self.create_task('psp_fixup', src=link_output, tgt=fixup_output) + task = self.create_task(finalobj_tool, src=fixup_output, tgt=finalobj_output) + task = self.create_task('psp_mksfo', tgt=mksfo_output) + task = self.create_task('psp_packpbp', src=[finalobj_output, mksfo_output], tgt=packpbp_output) + + if getattr(self, 'install_path', None): + if getattr(self, 'install_task', None): + self.install_task.inputs = self.install_task.outputs = [] + self.add_install_files(install_to=self.install_path, install_from=[packpbp_output, finalobj_output])