# -*- mode: python; coding: utf-8; -*-

#  Codezero -- a microkernel for embedded systems.
#
#  Copyright © 2009  B Labs Ltd
#
#  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.
#
#  You should have received a copy of the GNU General Public License along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.
#
#  Author: Russel Winder

Import('environment', 'previousImage', 'posixServicesDirectory')

taskName = 'test0'

def createTestExecLinkerScript(target, source, env):
    with open(target[0].path, 'w') as outFile:
        outFile.write('''
/*
 * Simple linker script for userspace or svc tasks.
 *
 * Copyright (C) 2007 Bahadir Balban
 */

/*
 * The only catch with this linker script is that everything
 * is linked starting at virtual_base, and loaded starting
 * at physical_base. virtual_base is the predefined region
 * of virtual memory for userland applications. physical_base
 * is determined at build-time, it is one of the subsequent pages
 * that come after the kernel image's load area.
 */
/* USER_AREA_START, see memlayout.h */
virtual_base = 0x10000000;
__stack = (0x20000000 - 0x1000 - 8); /* First page before the env/args */
INCLUDE "%s"

/* physical_base = 0x228000; */
offset = virtual_base - physical_base;

ENTRY(_start)

SECTIONS
{
	. = virtual_base;
	_start_text = .;
	.text : AT (ADDR(.text) - offset) { *(.text.init) *(.text) }
	/* rodata is needed else your strings will link at physical! */
	.rodata : AT (ADDR(.rodata) - offset) { *(.rodata) }
	.rodata1 : AT (ADDR(.rodata1) - offset) { *(.rodata1) }
	. = ALIGN(4K);
	.data : AT (ADDR(.data) - offset) { *(.data) }
	.bss : AT (ADDR(.bss) - offset) { *(.bss) }
	_end = .;
}
''' % ( source[0].path ))

def createTestExecS(target, source, env):
    with open(target[0].path, 'w') as outFile:
        outFile.write('''
.section .testexec

.align 4
.incbin "%s"
.align 4
''' % ( source[0].path ))

testTaskEnvironment = environment.Clone()
testTaskEnvironment.Append(CPPPATH=['#' + posixServicesDirectory +'libposix/include/posix'])
testExecLinkerScript = Command('#build/' + posixServicesDirectory + taskName + '/include/test_exec_linker.lds', testTaskEnvironment['physicalBaseLinkerScript'], createTestExecLinkerScript)
testExecEnvironment = testTaskEnvironment.Clone()
testExecEnvironment.Append(LINKFLAGS=['-T' + testExecLinkerScript[0].path])
testExec = testExecEnvironment.Program('test_exec', Glob('src/test_exec/*.[cS]'))
Depends(testExec, testExecLinkerScript)
testExecS = Command('#build/' + posixServicesDirectory + taskName + '/test_exec.S', testExec, createTestExecS)
program = testTaskEnvironment['buildTask'](taskName, Glob('*.c') + Glob('src/*.[cS]') + testExecS, testTaskEnvironment, previousImage)
Depends(program, testExec)

Return('program')
