summaryrefslogtreecommitdiff
path: root/infra/nncc
diff options
context:
space:
mode:
Diffstat (limited to 'infra/nncc')
-rw-r--r--infra/nncc/CMakeLists.txt150
-rw-r--r--infra/nncc/command/build11
-rw-r--r--infra/nncc/command/check-copyright62
-rw-r--r--infra/nncc/command/configure10
-rw-r--r--infra/nncc/command/docker-nncc10
-rw-r--r--infra/nncc/command/docker-run10
-rw-r--r--infra/nncc/command/docker-shell11
-rw-r--r--infra/nncc/command/test13
-rw-r--r--infra/nncc/config/build.configuration1
-rw-r--r--infra/nncc/config/docker.configuration46
10 files changed, 324 insertions, 0 deletions
diff --git a/infra/nncc/CMakeLists.txt b/infra/nncc/CMakeLists.txt
new file mode 100644
index 000000000..aa84391c8
--- /dev/null
+++ b/infra/nncc/CMakeLists.txt
@@ -0,0 +1,150 @@
+cmake_minimum_required(VERSION 3.1)
+
+project(nncc)
+
+enable_testing()
+
+set(CMAKE_CXX_STANDARD 11)
+
+set(CMAKE_SKIP_BUILD_RPATH FALSE)
+set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
+set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+# This feature works with CMake 3.5.2 or later. However, using previous versions does not produce
+# an error. We are still officially using CMake 3.1.0, but put this code for the sake of semantic
+# support in various development tools.
+# Todo: Someday, CMake needs to be updated to 3.7.2 or later to take advantage of improvements
+# such as `cmake-server`.
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+set(NNAS_PROJECT_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." CACHE
+ INTERNAL "Where to find nnas top-level source directory"
+)
+
+set(NNAS_EXTERNALS_DIR
+ "${NNAS_PROJECT_SOURCE_DIR}/externals" CACHE
+ INTERNAL "Where to download external dependencies"
+)
+set(NNCC_OVERLAY_DIR "${CMAKE_BINARY_DIR}/overlay" CACHE
+ INTERNAL "Where locally built external dependencies are installed")
+
+# Share package build script with runtime
+set(EXT_OVERLAY_DIR ${NNCC_OVERLAY_DIR})
+
+# This allows find_package to access configurations installed inside overlay
+list(APPEND CMAKE_PREFIX_PATH "${EXT_OVERLAY_DIR}")
+
+macro(nnas_include PREFIX)
+ include("${NNAS_PROJECT_SOURCE_DIR}/infra/cmake/modules/${PREFIX}.cmake")
+endmacro(nnas_include)
+
+macro(nnas_find_package PREFIX)
+ find_package(${PREFIX} CONFIG NO_DEFAULT_PATH
+ PATHS ${NNAS_PROJECT_SOURCE_DIR}/infra/cmake/packages
+ ${ARGN}
+ )
+endmacro(nnas_find_package)
+
+# nncc_find_resource(NAME) will update the following variables
+#
+# NAME_FOUND
+# NAME_DIR
+#
+# TODO Explain how to add a resource in README.md
+function(nncc_find_resource NAME)
+ set(RESOURCE_DIR "${NNAS_PROJECT_SOURCE_DIR}/res/${NAME}")
+
+ if(NOT IS_DIRECTORY ${RESOURCE_DIR})
+ set(${NAME}_FOUND FALSE PARENT_SCOPE)
+ return()
+ endif(NOT IS_DIRECTORY ${RESOURCE_DIR})
+
+ set(${NAME}_DIR ${RESOURCE_DIR} PARENT_SCOPE)
+ set(${NAME}_FOUND TRUE PARENT_SCOPE)
+endfunction(nncc_find_resource)
+
+###
+### CMake configuration
+###
+if(NOT CMAKE_BUILD_TYPE)
+ set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Type of build" FORCE)
+endif(NOT CMAKE_BUILD_TYPE)
+message(STATUS "Use '${CMAKE_BUILD_TYPE}' configuration")
+
+# Prefer -pthread to -lpthread for find_package(Threads ...)
+#
+# std::thread code compiled only with -lpthread emits the following runtime error (on GCC 4.8.4)
+#
+# terminate called after throwing an instance of 'std::system_error'
+# what(): Enable multithreading to use std::thread: Operation not permitted
+#
+set(THREADS_PREFER_PTHREAD_FLAG TRUE)
+
+###
+### Configuration
+###
+option(DOWNLOAD_PROTOBUF "Download Protocol Buffer source" ON)
+option(BUILD_PROTOBUF "Locally build Protocol Buffer from the downloaded source" ON)
+option(DOWNLOAD_EIGEN "Download Eigen source" ON)
+option(DOWNLOAD_FARMHASH "Download farmhash source" ON)
+option(DOWNLOAD_GEMMLOWP "Download GEMM low precesion library source" ON)
+option(DOWNLOAD_NEON2SSE "Download NEON2SSE library source" ON)
+option(DOWNLOAD_GFLAGS "Download GFlags source" OFF)
+option(DOWNLOAD_FLATBUFFERS "Download FlatBuffers source" ON)
+option(BUILD_FLATBUFFERS "Locally build Flatbuffers from the downloaded source" ON)
+option(DOWNLOAD_TENSORFLOW "Download TensorFlow source" ON)
+option(DOWNLOAD_CAFFE "Download Caffe source" ON)
+option(DOWNLOAD_PYTORCH "Download Pytorch source" ON)
+option(DOWNLOAD_ONNX "Download ONNX source" ON)
+option(DOWNLOAD_ABSEIL "Download Abseil-cpp source" ON)
+
+option(DOWNLOAD_GTEST "Download Google Test source" ON)
+option(BUILD_GTEST "Build Google Test from the downloaded source" ON)
+
+nnas_find_package(GTest QUIET)
+
+option(ENABLE_TEST "Build Tests using Google Test" ${GTest_FOUND})
+
+if(${ENABLE_TEST} AND NOT ${GTest_FOUND})
+ message(FATAL_ERROR "Google Test is required to enable test")
+endif(${ENABLE_TEST} AND NOT ${GTest_FOUND})
+
+option(ENABLE_COVERAGE "Build for coverage test" OFF)
+if(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
+ message(FATAL_ERROR "Test should be enabled to measure test coverage")
+endif(${ENABLE_COVERAGE} AND NOT ${ENABLE_TEST})
+
+if(${ENABLE_TEST})
+ include(CTest)
+endif(${ENABLE_TEST})
+
+option(ENABLE_STRICT_BUILD "Treat warning as error" OFF)
+
+# This option might be turned ON for Windows native build.
+# Check our ProtobufConfig.cmake for its usage.
+option(USE_PROTOBUF_LEGACY_IMPORT "Use legacy MODULE mode import rather than CONFIG mode" OFF)
+
+###
+### Target
+###
+add_library(nncc_common INTERFACE)
+if(ENABLE_STRICT_BUILD)
+ # TODO Remove -Wno-reoder
+ target_compile_options(nncc_common INTERFACE -Werror -Wall -Wextra -Wno-reorder)
+endif(ENABLE_STRICT_BUILD)
+
+add_library(nncc_coverage INTERFACE)
+if(ENABLE_COVERAGE)
+ target_compile_options(nncc_coverage INTERFACE -g -O0 -fprofile-arcs -ftest-coverage)
+ target_link_libraries(nncc_coverage INTERFACE gcov)
+endif(ENABLE_COVERAGE)
+
+###
+### Function
+###
+# TODO Remove this nnas_include
+nnas_include(OptionalTargetTools)
+nnas_include(AddSubdirectories)
+
+add_subdirectory("${NNAS_PROJECT_SOURCE_DIR}/compiler" "${CMAKE_BINARY_DIR}/compiler")
diff --git a/infra/nncc/command/build b/infra/nncc/command/build
new file mode 100644
index 000000000..86082c188
--- /dev/null
+++ b/infra/nncc/command/build
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+import "build.configuration"
+
+BUILD_WORKSPACE_PATH="${NNCC_PROJECT_PATH}/${BUILD_WORKSPACE_RPATH}"
+
+if [[ ! -d "${BUILD_WORKSPACE_PATH}" ]]; then
+ echo "'${BUILD_WORKSPACE_RPATH}' does not exist. Please run 'configure' first"
+ exit 255
+fi
+cd "${BUILD_WORKSPACE_PATH}" && cmake --build . -- "$@"
diff --git a/infra/nncc/command/check-copyright b/infra/nncc/command/check-copyright
new file mode 100644
index 000000000..b70785971
--- /dev/null
+++ b/infra/nncc/command/check-copyright
@@ -0,0 +1,62 @@
+#!/bin/bash
+
+# HOW TO USE
+#
+# Create .COPYRIGHT file at the root of your project (compiler/[PROJECT]/.COPYRIGHT)
+# with the copyright pattern required for your project.
+#
+# echo "Copyright (c) [0-9]\+ Samsung Electronics Co., Ltd. All Rights Reserved" > compiler/[PROJECT]/.COPYRIGHT
+#
+# DISCLAIMER
+#
+# This check works only when your copyright notice is of the following form:
+#
+# /**
+# * [Copyright notice]
+# ...
+# */
+#
+# NOTE
+#
+# The current implementation does not validate YEAR in the copyright notice.
+#
+# TODO Validate YEAR without FALSE POSTIVIES
+#
+# It already turns out that checking the initial commit year introduces
+# FALSE POSITIVES if there are relocated files.
+INVALID_FILES=()
+
+for COPYRIGHT_PATH in $(ls ${NNCC_PROJECT_PATH}/compiler/*/.COPYRIGHT); do
+ PROJECT_PATH="$(dirname ${COPYRIGHT_PATH})"
+ PROJECT_NAME="$(basename ${PROJECT_PATH})"
+
+ CANDIDATE_FILES=$(find "${PROJECT_PATH}" -iname '*.h' -o -iname '*.hpp' -o -iname '*.cpp' -o -iname '*.c')
+
+ # Skip copyright check if there is no candidate files
+ #
+ # NOTE "git ls-files" with no argument will enumerate all the files in the repo
+ if [[ -z ${CANDIDATE_FILES} ]]; then
+ continue
+ fi
+
+ for TRACKED_FILE in $(git ls-files $CANDIDATE_FILES); do
+ MATCHED=$(cat "${NNCC_PROJECT_PATH}/${TRACKED_FILE}" | head -n2 | tail -n1 | sed 's/^ \* //g' | grep -f "${COPYRIGHT_PATH}" | wc -l)
+
+ if [[ ${MATCHED} -ne 1 ]]; then
+ INVALID_FILES+=(${TRACKED_FILE})
+ fi
+ done
+done
+
+if [[ ${#INVALID_FILES[@]} -ne 0 ]]; then
+ echo ">> FAILED <<"
+ echo
+ echo "PLEASE CHECK THE FOLLOWING FILES"
+ for INVALID_FILE in "${INVALID_FILES[@]}"; do
+ echo "- ${INVALID_FILE}"
+ done
+ exit 255
+fi
+
+echo ">> PASSED <<"
+exit 0
diff --git a/infra/nncc/command/configure b/infra/nncc/command/configure
new file mode 100644
index 000000000..2648cb893
--- /dev/null
+++ b/infra/nncc/command/configure
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+import "build.configuration"
+
+BUILD_WORKSPACE_PATH="${NNCC_PROJECT_PATH}/${BUILD_WORKSPACE_RPATH}"
+
+mkdir -p "${BUILD_WORKSPACE_PATH}"
+
+cd "${BUILD_WORKSPACE_PATH}"
+cmake "${NNCC_PROJECT_PATH}/infra/nncc" "$@"
diff --git a/infra/nncc/command/docker-nncc b/infra/nncc/command/docker-nncc
new file mode 100644
index 000000000..0eea016c6
--- /dev/null
+++ b/infra/nncc/command/docker-nncc
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+import "docker.configuration"
+
+docker run $DOCKER_RUN_OPTS $DOCKER_ENV_VARS $DOCKER_VOLUMES $DOCKER_IMAGE_NAME ./nncc "$@"
+EXITCODE=$?
+
+docker_cleanup
+
+exit $EXITCODE
diff --git a/infra/nncc/command/docker-run b/infra/nncc/command/docker-run
new file mode 100644
index 000000000..863b2b8f1
--- /dev/null
+++ b/infra/nncc/command/docker-run
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+import "docker.configuration"
+
+docker run $DOCKER_RUN_OPTS $DOCKER_ENV_VARS $DOCKER_VOLUMES $DOCKER_IMAGE_NAME "$@"
+EXITCODE=$?
+
+docker_cleanup
+
+exit $EXITCODE
diff --git a/infra/nncc/command/docker-shell b/infra/nncc/command/docker-shell
new file mode 100644
index 000000000..7f8449855
--- /dev/null
+++ b/infra/nncc/command/docker-shell
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+import "docker.configuration"
+
+DOCKER_RUN_OPTS+=" -it"
+docker run $DOCKER_RUN_OPTS $DOCKER_ENV_VARS $DOCKER_VOLUMES $DOCKER_IMAGE_NAME /bin/bash
+EXITCODE=$?
+
+docker_cleanup
+
+exit $EXITCODE
diff --git a/infra/nncc/command/test b/infra/nncc/command/test
new file mode 100644
index 000000000..96ddd7a17
--- /dev/null
+++ b/infra/nncc/command/test
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+import "build.configuration"
+
+BUILD_WORKSPACE_PATH="${NNCC_PROJECT_PATH}/${BUILD_WORKSPACE_RPATH}"
+
+if [[ ! -d "${BUILD_WORKSPACE_PATH}" ]]; then
+ echo "'${BUILD_WORKSPACE_RPATH}' does not exist. Please run 'configure' first"
+ exit 255
+fi
+
+export CTEST_OUTPUT_ON_FAILURE=1
+cd "${BUILD_WORKSPACE_PATH}" && ctest "$@"
diff --git a/infra/nncc/config/build.configuration b/infra/nncc/config/build.configuration
new file mode 100644
index 000000000..25ffb6ee0
--- /dev/null
+++ b/infra/nncc/config/build.configuration
@@ -0,0 +1 @@
+BUILD_WORKSPACE_RPATH=${NNCC_WORKSPACE:-build}
diff --git a/infra/nncc/config/docker.configuration b/infra/nncc/config/docker.configuration
new file mode 100644
index 000000000..7078585a2
--- /dev/null
+++ b/infra/nncc/config/docker.configuration
@@ -0,0 +1,46 @@
+DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME:-nnas}
+echo "Using docker image ${DOCKER_IMAGE_NAME}"
+
+if [ -z "`docker images ${DOCKER_IMAGE_NAME}`" ]; then
+ echo "Need docker image!"
+ exit 1
+fi
+
+HOST_PATH="$NNCC_PROJECT_PATH"
+DOCKER_PATH="$NNCC_PROJECT_PATH"
+
+export GIT_SSL_NO_VERIFY=1
+
+DOCKER_VOLUMES=" -v $HOST_PATH:$DOCKER_PATH"
+
+DOCKER_ENV_VARS+=" -e http_proxy"
+DOCKER_ENV_VARS+=" -e no_proxy"
+DOCKER_ENV_VARS+=" -e GIT_SSL_NO_VERIFY"
+DOCKER_ENV_VARS+=" -e CAFFE_URL"
+DOCKER_ENV_VARS+=" -e GTEST_URL"
+DOCKER_ENV_VARS+=" -e EIGEN_URL"
+DOCKER_ENV_VARS+=" -e GEMMLOWP_URL"
+DOCKER_ENV_VARS+=" -e FLATBUFFERS_URL"
+DOCKER_ENV_VARS+=" -e FARMHASH_URL"
+DOCKER_ENV_VARS+=" -e NEON2SSE_URL"
+DOCKER_ENV_VARS+=" -e TENSORFLOW_URL"
+
+DOCKER_ENV_VARS+=" -e NNCC_WORKSPACE"
+
+DOCKER_RUN_OPTS="${DOCKER_OPTS}"
+DOCKER_RUN_OPTS+=" --rm"
+DOCKER_RUN_OPTS+=" -w $DOCKER_PATH"
+
+function docker_cleanup()
+{
+ # Newly created files during during docker run can have different ownership.
+ # This may cause some problems, for example, some jenkins slaves or developers
+ # can't remove built files due to lack of permission.
+ # To address this issue, let's change owner of all files
+ # in nncc to owner of nncc.
+ NNCC_OWNER_UID=$(stat -c "%u" $HOST_PATH)
+ NNCC_OWNER_GID=$(stat -c "%g" $HOST_PATH)
+
+ CMD="chown -R $NNCC_OWNER_UID:$NNCC_OWNER_GID $DOCKER_PATH"
+ docker run $DOCKER_RUN_OPTS $DOCKER_ENV_VARS $DOCKER_VOLUMES $DOCKER_IMAGE_NAME $CMD
+}