Improvements to building CKW

* Always link Compute Kernel Writer statically to Compute Library

* Move CMake logic to be set on libckw target

* Build CKW in parallel from SCons

Resolves: COMPMID-6297

Change-Id: I247a1f6ddf84a58032358a196574866b857d9bdc
Signed-off-by: Jakub Sujak <jakub.sujak@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9834
Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
diff --git a/SConscript b/SConscript
index c9a56ed..c713955 100644
--- a/SConscript
+++ b/SConscript
@@ -31,8 +31,12 @@
 import json
 import codecs
 
-print("DEPRECATION NOTICE: Legacy libarm_compute_core has been deprecated. Link your application only to "
-      "libarm_compute for core library functionality")
+from SCons.Warnings import warn, DeprecatedWarning
+
+warn(DeprecatedWarning,
+     "DEPRECATION NOTICE: Legacy libarm_compute_core has been deprecated and is scheduled for removal in 24.02 release."
+     " Link your application only to libarm_compute for core library functionality"
+     )
 
 VERSION = "v0.0-unreleased"
 LIBRARY_VERSION_MAJOR = 31
@@ -123,6 +127,9 @@
         cloned_build_env["LINKFLAGS"].remove('-pie')
         cloned_build_env["LINKFLAGS"].remove('-static-libstdc++')
 
+    if env['ckw']:
+        libs.append('libckw.a')
+
     if static:
         obj = cloned_build_env.StaticLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs)
     else:
diff --git a/SConstruct b/SConstruct
index 419fa33..48f1747 100644
--- a/SConstruct
+++ b/SConstruct
@@ -423,7 +423,7 @@
 
 """Build the Compute Kernel Writer subproject"""
 if env['ckw']:
-    # Strip ccache from CC and CXX
+    # Strip ccache prefix from CC and CXX to obtain only the target triple
     CKW_CC = env['CC'].replace(env['compiler_cache'] + " ", "")
     CKW_CXX = env['CXX'].replace(env['compiler_cache'] + " ", "")
     CKW_CCACHE = 1 if env['compiler_cache'] else 0
@@ -452,20 +452,17 @@
                                                         CKW_CCACHE=CKW_CCACHE
                                                         )
 
-    CKW_CMAKE_CONFIGURE_STATIC = CKW_CMAKE_CMD + "-DBUILD_SHARED_LIBS=OFF"
-    CKW_CMAKE_CONFIGURE_SHARED = CKW_CMAKE_CMD + "-DBUILD_SHARED_LIBS=ON"
-    CKW_CMAKE_BUILD = "cmake --build {CKW_BUILD_DIR}".format(CKW_BUILD_DIR=CKW_BUILD_DIR)
+    # Configure CKW static objects with -fPIC (CMAKE_POSITION_INDEPENDENT_CODE) option to enable linking statically to ACL
+    CKW_CMAKE_CONFIGURE_STATIC = CKW_CMAKE_CMD + "-DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON"
+    CKW_CMAKE_BUILD = "cmake --build {CKW_BUILD_DIR} -j{NUM_JOBS}".format(CKW_BUILD_DIR=CKW_BUILD_DIR,
+                                                                          NUM_JOBS=GetOption('num_jobs')
+                                                                          )
 
     # Build Compute Kernel Writer Static Library
     subprocess.check_call(CKW_CMAKE_CONFIGURE_STATIC, stderr=subprocess.STDOUT, shell=True)
     subprocess.check_call(CKW_CMAKE_BUILD, stderr=subprocess.STDOUT, shell=True)
 
-    # Build Compute Kernel Writer Shared Library
-    subprocess.check_call(CKW_CMAKE_CONFIGURE_SHARED, stderr=subprocess.STDOUT, shell=True)
-    subprocess.check_call(CKW_CMAKE_BUILD, stderr=subprocess.STDOUT, shell=True)
-
-    # Linking library
-    env.Append(LIBS = ['ckw'])
+    # Let ACL know where to find CKW headers
     env.Append(CPPPATH = CKW_INCLUDE_DIR)
 
 if not GetOption("help"):
diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt
index 4bf8494..2c770e4 100644
--- a/compute_kernel_writer/CMakeLists.txt
+++ b/compute_kernel_writer/CMakeLists.txt
@@ -28,11 +28,10 @@
 project(ComputeKernelWriter
     VERSION 1.0.0
     LANGUAGES CXX
-    )
+)
 
 set(CMAKE_CXX_STANDARD 14)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
-set(CMAKE_CXX_EXTENSIONS OFF)
 
 include(GNUInstallDirs)
 
@@ -41,17 +40,11 @@
 #---------------------------------------------------------------------
 # Options
 
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wdisabled-optimization -Wformat=2 \
-                     -Winit-self -Wstrict-overflow=2 -Wswitch-default -Woverloaded-virtual \
-                     -Wformat-security -Wctor-dtor-privacy -Wsign-promo -Weffc++ \
-                     -Wlogical-op -Wstrict-null-sentinel")
-set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os")
-
 option(CKW_ENABLE_OPENCL "Enable OpenCL code generation" OFF)
 option(CKW_ENABLE_ASSERTS "Enable assertions. Always enabled in Debug builds" OFF)
 option(CKW_BUILD_TESTING "Build the Compute Kernel Writer validation test suite" OFF)
 option(CKW_BUILD_EXAMPLES "Build the Compute Kernel Writer examples" OFF)
-option(CKW_CCACHE "Enable compiler cache builds" OFF)
+option(CKW_CCACHE "Use compiler cache for faster recompilation" OFF)
 
 #---------------------------------------------------------------------
 # Build configuration
@@ -79,25 +72,49 @@
 #---------------------------------------------------------------------
 # Library targets
 
-set(CKW_ASSERTS_OPTS "-fstack-protector-strong")
+set(CKW_CXX_FLAGS
+    -Wall
+    -Werror
+    -Wextra
+    -Wdisabled-optimization
+    -Wformat=2
+    -Winit-self
+    -Wstrict-overflow=2
+    -Wswitch-default
+    -Woverloaded-virtual
+    -Wformat-security
+    -Wctor-dtor-privacy
+    -Wsign-promo
+    -Weffc++
+    -pedantic
+)
+set(GNU_WARNINGS
+    -Wlogical-op
+    -Wstrict-null-sentinel
+)
+set(CKW_ASSERTS_OPTS
+    -fstack-protector-strong
+)
 
-# Define common properties across all targets
-add_library(ckw_common INTERFACE)
+add_library(ckw)
+target_compile_options(ckw
+    PUBLIC
+    ${CKW_CXX_FLAGS}
+    "$<$<CXX_COMPILER_ID:GNU>:${GNU_WARNINGS}>"
+    "$<$<CONFIG:Debug>:${CKW_ASSERTS_OPTS}>"
+    "$<$<BOOL:${CKW_ASSERTS}>:${CKW_ASSERTS_OPTS}>"
+    # Set CMAKE_CXX_FLAGS last so user can overwrite options
+    ${CMAKE_CXX_FLAGS}
+    PRIVATE
+    $<$<CONFIG:Release>:-Os>
+)
 
-target_compile_definitions(ckw_common INTERFACE
+target_compile_definitions(ckw PUBLIC
     $<$<CONFIG:Debug>:COMPUTE_KERNEL_WRITER_DEBUG_ENABLED>
     $<$<CONFIG:Debug>:COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED>
     $<$<BOOL:${CKW_ASSERTS}>:COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED>
     $<$<BOOL:${CKW_ENABLE_OPENCL}>:COMPUTE_KERNEL_WRITER_OPENCL_ENABLED>
-    )
-
-target_compile_options(ckw_common INTERFACE
-    -pedantic
-    "$<$<BOOL:${CKW_ASSERTS}>:${CKW_ASSERTS_OPTS}>"
-    )
-
-# Compute Kernel Writer library
-add_library(ckw)
+)
 
 target_sources(ckw PRIVATE
     src/Error.cpp
@@ -124,10 +141,9 @@
         src/cl/CLHelpers.cpp
         src/cl/CLTile.cpp
         src/cl/ICLTile.cpp
-        )
+    )
 endif()
 
-target_link_libraries(ckw PUBLIC ckw_common)
 target_include_directories(ckw
     PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include
     PRIVATE ${CMAKE_CURRENT_LIST_DIR}
@@ -142,30 +158,30 @@
         validation/tests/TensorBitMaskTest.hpp
         validation/tests/UtilsTest.hpp
         validation/Validation.cpp
-        )
+    )
     if(CKW_ENABLE_OPENCL)
-    target_sources(ckw_validation PRIVATE
-        validation/tests/CLConstantTileTest.hpp
-        validation/tests/CLTileTest.hpp)
+        target_sources(ckw_validation PRIVATE
+            validation/tests/CLConstantTileTest.hpp
+            validation/tests/CLTileTest.hpp)
     endif()
 
     target_link_libraries(ckw_validation PRIVATE ckw)
     target_include_directories(ckw_validation
         PRIVATE ${CMAKE_CURRENT_LIST_DIR}
-        )
+    )
 endif()
 
 #---------------------------------------------------------------------
-# Example
+# Examples
+
+function(add_ckw_example name)
+    add_executable(${name} ${ARGN})
+    target_link_libraries(${name} PUBLIC ckw)
+endfunction(add_ckw_example)
 
 if(CKW_BUILD_EXAMPLES)
-    add_executable(ckw_example_add_exp_store
-        examples/add_exp_store.cpp
-    )
-
-    target_link_libraries(ckw_example_add_exp_store
-        PUBLIC ckw
-    )
+    add_ckw_example(ckw_example_add_exp_store
+        examples/add_exp_store.cpp)
 endif()
 
 #---------------------------------------------------------------------
@@ -176,8 +192,8 @@
     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
     ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
-    )
+)
 
 install(DIRECTORY include/ckw
     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
-    )
+)