COMPMID-2706: Add the ability to build bootcode for bare metal

Adds an option for providing a linker script and also adds the
bootcode folder, it it exists, in the build path and links it
with the executable binaries.

Change-Id: I4119b21bdf1b4dd7fe38c4ee66741460666f53a1
Signed-off-by: Michalis Spyrou <michalis.spyrou@arm.com>
Reviewed-on: https://review.mlplatform.org/c/2051
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
diff --git a/tests/SConscript b/tests/SConscript
index 6632169..0c56a74 100644
--- a/tests/SConscript
+++ b/tests/SConscript
@@ -73,6 +73,9 @@
     test_env.Append(LIBS = ["arm_compute_graph", "arm_compute", "arm_compute_core"])
     arm_compute_lib = arm_compute_graph_so
 
+if env['os'] in ['bare_metal']:
+    Import("bootcode_o")
+
 #FIXME Delete before release
 if env['internal_only']:
     test_env.Append(CPPDEFINES=['INTERNAL_ONLY'])
@@ -121,8 +124,14 @@
     if env['internal_only']:
         files_benchmark += Glob('../3rdparty/tests/benchmark/NEON/' + filter_pattern)
 
-    files_validation += Glob('validation/NEON/*/' + filter_pattern)
     files_validation += Glob('validation/NEON/' + filter_pattern)
+    if env['os'] == 'bare_metal':
+        files_validation += Glob('validation/NEON/UNIT/MemoryManager.cpp' + filter_pattern)
+        files_validation += Glob('validation/NEON/UNIT/DynamicTensor.cpp' + filter_pattern)
+        files_validation += Glob('validation/NEON/UNIT/TensorAllocator.cpp' + filter_pattern)
+    else:
+        files_validation += Glob('validation/NEON/*/' + filter_pattern)
+
 
 if env['gles_compute']:
     test_env.Append(CPPDEFINES=['ARM_COMPUTE_GC'])
@@ -146,12 +155,22 @@
     Default(arm_compute_benchmark)
     Export('arm_compute_benchmark')
 
+bm_link_flags = []
+if test_env['linker_script']:
+    bm_link_flags = ['-Wl,--build-id=none', '-T', env['linker_script']]
+
 if test_env['validation_tests']:
     arm_compute_validation_framework = env.StaticLibrary('arm_compute_validation_framework', Glob('validation/reference/*.cpp') + Glob('validation/*.cpp'), LIBS= [ arm_compute_test_framework, arm_compute_core_a])
     Depends(arm_compute_validation_framework , arm_compute_test_framework)
     Depends(arm_compute_validation_framework , arm_compute_core_a)
 
-    arm_compute_validation = test_env.Program('arm_compute_validation', files_validation + common_objects, LIBS=[arm_compute_validation_framework] + test_env['LIBS'])
+    program_objects = files_validation + common_objects
+    if test_env['os'] == 'bare_metal':
+        Depends(arm_compute_validation_framework , bootcode_o)
+        program_objects += bootcode_o
+
+
+    arm_compute_validation = test_env.Program('arm_compute_validation', program_objects, LIBS=[arm_compute_validation_framework] + test_env['LIBS'], LINKFLAGS=test_env['LINKFLAGS'] + bm_link_flags)
     arm_compute_validation = install_bin(arm_compute_validation)
     Depends(arm_compute_validation, arm_compute_validation_framework)
     Depends(arm_compute_validation, arm_compute_test_framework)
@@ -162,11 +181,14 @@
 
     if test_env['validate_examples']:
         files_validate_examples = [ test_env.Object('validate_examples/RunExample.cpp') ] + [ x for x in common_objects if not "main.o" in str(x)]
+        if test_env['os'] == 'bare_metal':
+            files_validate_examples += bootcode_o
+
         arm_compute_validate_examples = []
         if test_env['neon']:
             for file in Glob("validate_examples/neon_*.cpp"):
                 example = "validate_" + os.path.basename(os.path.splitext(str(file))[0])
-                arm_compute_validate_examples += [ test_env.Program(example, [ test_env.Object(source=file, target=example) ] + files_validate_examples, LIBS = [ arm_compute_validation_framework]) ]
+                arm_compute_validate_examples += [ test_env.Program(example, [ test_env.Object(source=file, target=example) ] + files_validate_examples, LIBS = [ arm_compute_validation_framework], LINKFLAGS=test_env['LINKFLAGS'] + bm_link_flags) ]
         if test_env['opencl']:
             cl_examples = []
             files = Glob("validate_examples/cl_*.cpp")
@@ -181,7 +203,7 @@
                 for file in Glob("validate_examples/graph_*.cpp"):
                     example = "validate_" + os.path.basename(os.path.splitext(str(file))[0])
                     if env['os'] in ['android', 'bare_metal'] or env['standalone']:
-                        prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils]+ files_validate_examples, LIBS = test_env["LIBS"] + [ arm_compute_validation_framework ], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--whole-archive',arm_compute_lib,'-Wl,--no-whole-archive'])
+                        prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils]+ files_validate_examples, LIBS = test_env["LIBS"] + [ arm_compute_validation_framework ], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--whole-archive',arm_compute_lib,'-Wl,--no-whole-archive'] + bm_link_flags)
                         arm_compute_validate_examples += [ prog ]
                     else:
                         #-Wl,--allow-shlib-undefined: Ignore dependencies of dependencies
@@ -196,6 +218,8 @@
 
 if test_env['benchmark_examples']:
     files_benchmark_examples = test_env.Object('benchmark_examples/RunExample.cpp')
+    if test_env['os'] == 'bare_metal':
+        files_benchmark_examples += bootcode_o
     graph_utils = test_env.Object(source="../utils/GraphUtils.cpp", target="GraphUtils")
     graph_params = test_env.Object(source="../utils/CommonGraphOptions.cpp", target="CommonGraphOptions")
     arm_compute_benchmark_examples = []
@@ -203,7 +227,7 @@
         if test_env['neon']:
             for file in Glob("%s/neon_*.cpp" % examples_folder):
                 example = "benchmark_" + os.path.basename(os.path.splitext(str(file))[0])
-                arm_compute_benchmark_examples += [ test_env.Program(example, [ test_env.Object(source=file, target=example) ] + files_benchmark_examples) ]
+                arm_compute_benchmark_examples += [ test_env.Program(example, [ test_env.Object(source=file, target=example) ] + files_benchmark_examples, LINKFLAGS=test_env["LINKFLAGS"]+ bm_link_flags) ]
         if test_env['opencl']:
             cl_examples = []
             files = Glob("%s/cl_*.cpp" % examples_folder)
@@ -228,11 +252,11 @@
         for file in Glob("%s/graph_*.cpp" % examples_folder ):
             example = "benchmark_" + os.path.basename(os.path.splitext(str(file))[0])
             if env['os'] in ['android', 'bare_metal'] or env['standalone']:
-                prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils, graph_params]+ files_benchmark_examples, LIBS = test_env["LIBS"], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--whole-archive',arm_compute_lib,'-Wl,--no-whole-archive'])
+                prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils, graph_params]+ files_benchmark_examples, LIBS = test_env["LIBS"], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--whole-archive',arm_compute_lib,'-Wl,--no-whole-archive'] + bm_link_flags)
                 arm_compute_benchmark_examples += [ prog ]
             else:
                 #-Wl,--allow-shlib-undefined: Ignore dependencies of dependencies
-                prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils, graph_params]+ files_benchmark_examples, LIBS = test_env["LIBS"] + ["arm_compute_graph"], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--allow-shlib-undefined'] )
+                prog = test_env.Program(example, [ test_env.Object(source=file, target=example), graph_utils, graph_params]+ files_benchmark_examples, LIBS = test_env["LIBS"] + ["arm_compute_graph"], LINKFLAGS=test_env["LINKFLAGS"]+['-Wl,--allow-shlib-undefined'])
                 arm_compute_benchmark_examples += [ prog ]
     arm_compute_benchmark_examples = install_bin(arm_compute_benchmark_examples)
     Depends(arm_compute_benchmark_examples, arm_compute_test_framework)
diff --git a/tests/benchmark_examples/RunExample.cpp b/tests/benchmark_examples/RunExample.cpp
index 5c9dd71..f3de308 100644
--- a/tests/benchmark_examples/RunExample.cpp
+++ b/tests/benchmark_examples/RunExample.cpp
@@ -163,8 +163,12 @@
     }
     framework.set_throw_errors(options.throw_errors->value());
     arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
-    framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
 
+#ifdef BARE_METAL
+    framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
+#else  /* BARE_METAL */
+    framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
+#endif /* BARE_METAL */
     //func(argc, argv);
     bool success = framework.run();
     if(options.log_level->value() > framework::LogLevel::NONE)
diff --git a/tests/validation/CPP/DetectionPostProcessLayer.cpp b/tests/validation/CPP/DetectionPostProcessLayer.cpp
index 51f3452..f4528fb 100644
--- a/tests/validation/CPP/DetectionPostProcessLayer.cpp
+++ b/tests/validation/CPP/DetectionPostProcessLayer.cpp
@@ -89,7 +89,7 @@
         }
         else
         {
-            uint8_offset = static_cast<uint8_t>(std::round(f_offset));
+            uint8_offset = static_cast<uint8_t>(arm_compute::support::cpp11::round(f_offset));
         }
         offset = uint8_offset;
     }
diff --git a/tests/validation/NEON/DetectionPostProcessLayer.cpp b/tests/validation/NEON/DetectionPostProcessLayer.cpp
index f479a13..4413ed4 100644
--- a/tests/validation/NEON/DetectionPostProcessLayer.cpp
+++ b/tests/validation/NEON/DetectionPostProcessLayer.cpp
@@ -89,7 +89,7 @@
         }
         else
         {
-            uint8_offset = static_cast<uint8_t>(std::round(f_offset));
+            uint8_offset = static_cast<uint8_t>(support::cpp11::round(f_offset));
         }
         offset = uint8_offset;
     }
diff --git a/tests/validation/reference/ElementWiseUnary.cpp b/tests/validation/reference/ElementWiseUnary.cpp
index 06beb2a..eaaaa4e 100644
--- a/tests/validation/reference/ElementWiseUnary.cpp
+++ b/tests/validation/reference/ElementWiseUnary.cpp
@@ -59,7 +59,7 @@
                 dst[i] = std::sin(src[i]);
                 break;
             case ElementWiseUnary::ROUND:
-                dst[i] = std::nearbyint(src[i]);
+                dst[i] = arm_compute::support::cpp11::nearbyint(src[i]);
                 break;
             default:
                 ARM_COMPUTE_ERROR("Not implemented");