build automation: replace genapisamples.py with script function

This commit is contained in:
nakst 2022-03-02 20:51:35 +00:00
parent c2db2e2de3
commit 10a7d4f1d1
4 changed files with 52 additions and 44 deletions

View File

@ -1,39 +0,0 @@
import os
for config_file in os.listdir('apps/samples'):
if config_file.endswith('.ini'):
source_file = config_file.split('.')[0] + '.c'
if not os.path.exists('apps/samples/' + source_file):
source_file = config_file.split('.')[0] + '.cpp'
if not os.path.exists('apps/samples/' + source_file):
continue
application_name = None
with open('apps/samples/' + config_file, 'r') as config_fd:
config = config_fd.read().split('\n')
config_section = ''
for config_line in config:
if len(config_line) > 0 and config_line[0] == '[':
config_section = config_line
elif config_section == '[general]' and config_line.startswith('name='):
application_name = config_line[5:]
if not application_name:
continue
print(application_name, source_file, config_file)
folder = 'root/API Samples/' + application_name
try:
os.mkdir(folder)
except:
pass
with open('apps/samples/' + source_file, 'r') as source_fd:
with open(folder + '/' + source_file, 'w') as source_dest_fd:
source_dest_fd.write(source_fd.read())
with open('apps/samples/' + config_file, 'r') as config_fd:
with open(folder + '/make.build_core', 'w') as config_dest_fd:
config = config_fd.read().split('\n')
config_section = ''
for config_line in config:
if len(config_line) > 0 and config_line[0] == '[':
config_section = config_line
if config_section == '[build]' and config_line.startswith('source='):
config_dest_fd.write('source=' + source_file + '\n')
else:
config_dest_fd.write(config_line + '\n')

View File

@ -496,7 +496,7 @@ char baseModuleSource[] = {
" return StringSlice(string, start, end);"
"}"
"str[] StringSplitByCharacter(str string, str character, bool includeEmptyString) {"
"\n assert character:len() == 1;\n"
" assert character:len() == 1;"
" str[] list = new str[];"
" int x = 0;"
" for int i = 0; i < string:len(); i += 1 {"
@ -516,6 +516,8 @@ char baseModuleSource[] = {
" }"
" return c;"
"}"
"bool StringEndsWith(str s, str x) { return s:len() >= x:len() && StringSlice(s, s:len() - x:len(), s:len()) == x; }"
"bool StringStartsWith(str s, str x) { return s:len() >= x:len() && StringSlice(s, 0, x:len()) == x; }"
"bool CharacterIsAlnum(str c) {"
" int b = CharacterToByte(c);"
" return (b >= CharacterToByte(\"A\") && b <= CharacterToByte(\"Z\")) || (b >= CharacterToByte(\"a\") && b <= CharacterToByte(\"z\"))"
@ -4229,6 +4231,7 @@ uintptr_t HeapAllocate(ExecutionContext *context) {
for (uintptr_t i = 1; i < context->heapEntriesAllocated; i++) {
if (!context->heap[i].gcMark) {
// PrintDebug("\033[0;32mFreeing index %d...\033[0m\n", i);
HeapFreeEntry(context, i);
*link = i;
link = &context->heap[i].nextUnusedEntry;
@ -4367,6 +4370,7 @@ int ScriptExecuteFunction(uintptr_t instructionPointer, ExecutionContext *contex
while (true) {
uint8_t command = functionData[instructionPointer++];
// PrintDebug("--> %d, %ld, %ld, %ld\n", command, instructionPointer - 1, context->c->id, context->c->stackPointer);
// PrintBackTrace(context, instructionPointer - 1, context->c, "");
if (command == T_BLOCK || command == T_FUNCBODY) {
uint16_t newVariableCount = functionData[instructionPointer + 0] + (functionData[instructionPointer + 1] << 8);
@ -5513,6 +5517,11 @@ int ScriptExecuteFunction(uintptr_t instructionPointer, ExecutionContext *contex
if (isErr) {
if (result != EXTCALL_RETURN_ERR_ERROR || returnValue.i) {
// Temporarily put the return value on the stack in case garbage collection occurs
// in the following HeapAllocate (i.e. before the return value has been wrapped).
context->c->stackIsManaged[context->c->stackPointer] = result != EXTCALL_RETURN_ERR_UNMANAGED;
context->c->stack[context->c->stackPointer++] = returnValue;
// TODO Handle memory allocation failures here.
uintptr_t index = HeapAllocate(context);
context->heap[index].type = T_ERR;
@ -5520,6 +5529,8 @@ int ScriptExecuteFunction(uintptr_t instructionPointer, ExecutionContext *contex
context->heap[index].internalValuesAreManaged = result != EXTCALL_RETURN_ERR_UNMANAGED;
context->heap[index].errorValue = returnValue;
returnValue.i = index;
context->c->stackPointer--;
} else {
// Unknown error.
returnValue.i = 0;

View File

@ -58,7 +58,7 @@ void Start() {
}
void GenerateOVF() {
str[] template = StringSplitByCharacter(FileReadAll("util/automation/template.ovf"):assert(), "$", true);
str[] template = StringSplitByCharacter(FileReadAll("util/template.ovf"):assert(), "$", true);
assert template:len() == 5;
str uuid1 = UUIDGenerate();
str uuid2 = UUIDGenerate();
@ -80,6 +80,44 @@ void DeleteUnneededDirectoriesForDebugInfo() {
PathDeleteRecursively(".git");
}
void GenerateAPISamples() {
str sourceFolder = "apps/samples/";
str destinationFolder = "root/API Samples/";
for str configFile in DirectoryEnumerateChildren(sourceFolder):assert() {
if StringEndsWith(configFile, ".ini") {
str[] config = StringSplitByCharacter(FileReadAll(sourceFolder + configFile):assert(), "\n", false);
str sourceFile;
str applicationName;
str section;
for str line in config {
if line[0] == "[" section = line;
if section == "[general]" && StringStartsWith(line, "name=") applicationName = StringSlice(line, 5, line:len());
if section == "[build]" && StringStartsWith(line, "source=") sourceFile = StringSlice(line, 7, line:len());
}
assert applicationName != "";
assert StringStartsWith(sourceFile, "apps/samples/");
sourceFile = StringSlice(sourceFile, 13, sourceFile:len());
Log("%applicationName%, %sourceFile%, %configFile%");
str folder = destinationFolder + applicationName + "/";
assert PathCreateLeadingDirectories(folder);
assert FileCopy(sourceFolder + sourceFile, folder + sourceFile);
for int i = 0; i < config:len(); i += 1 {
str line = config[i];
if line[0] == "[" section = line;
if section == "[build]" && StringStartsWith(line, "source=") config[i] = "source=" + sourceFile;
}
FileWriteAll(folder + "make.build_core", StringJoin(config, "\n", false));
}
}
}
void AutomationBuild() {
// TODO:
// Copy the source onto the drive for self hosting.
@ -113,9 +151,7 @@ void AutomationBuild() {
assert FileCopy("res/Flip.img", "root/Demo Content/Flip.img");
assert FileCopy("res/Teapot.obj", "root/Demo Content/Teapot.obj");
assert FileCopy("res/Fonts/Atkinson Hyperlegible Regular.ttf", "root/Demo Content/Atkinson Hyperlegible Regular.ttf");
// Copy API samples.
assert SystemShellExecute("python util/automation/genapisamples.py"); // TODO Replace.
GenerateAPISamples();
// Enable extra applications and build them.
assert FileWriteAll("bin/extra_applications.ini", "util/designer2.ini\n"