From 85bfb24aa98a4c963ee36f87c6f3af779dffc5ab Mon Sep 17 00:00:00 2001
From: Leonard Lausen <lausen@amazon.com>
Date: Sat, 25 Jan 2020 23:41:25 +0000
Subject: [PATCH 1/3] cmake staticbuild

---
 CMakeLists.txt                        | 15 +++++-
 ci/docker/install/ubuntu_publish.sh   | 23 +++++++++-
 ci/docker/runtime_functions.sh        | 18 ++++++++
 ci/jenkins/Jenkins_steps.groovy       | 26 +++++++++++
 ci/jenkins/Jenkinsfile_unix_cpu       |  1 +
 ci/jenkins/Jenkinsfile_unix_gpu       |  1 +
 config/distribution/linux_cpu.cmake   | 28 ++++++++++++
 config/distribution/linux_cu100.cmake | 32 +++++++++++++
 config/distribution/linux_cu101.cmake | 32 +++++++++++++
 config/distribution/linux_cu102.cmake | 32 +++++++++++++
 config/distribution/linux_cu75.cmake  | 32 +++++++++++++
 config/distribution/linux_cu80.cmake  | 32 +++++++++++++
 config/distribution/linux_cu90.cmake  | 32 +++++++++++++
 config/distribution/linux_cu91.cmake  | 32 +++++++++++++
 config/distribution/linux_cu92.cmake  | 32 +++++++++++++
 tools/dependencies/openblas.sh        | 23 ++++++++--
 tools/dependencies/opencv.sh          |  2 +-
 tools/pip/setup.py                    | 14 ++++--
 tools/staticbuild/README.md           | 25 ++++++++--
 tools/staticbuild/build.sh            |  6 ++-
 tools/staticbuild/build_lib.sh        | 14 +++++-
 tools/staticbuild/build_lib_cmake.sh  | 66 +++++++++++++++++++++++++++
 22 files changed, 498 insertions(+), 20 deletions(-)
 create mode 100644 config/distribution/linux_cpu.cmake
 create mode 100644 config/distribution/linux_cu100.cmake
 create mode 100644 config/distribution/linux_cu101.cmake
 create mode 100644 config/distribution/linux_cu102.cmake
 create mode 100644 config/distribution/linux_cu75.cmake
 create mode 100644 config/distribution/linux_cu80.cmake
 create mode 100644 config/distribution/linux_cu90.cmake
 create mode 100644 config/distribution/linux_cu91.cmake
 create mode 100644 config/distribution/linux_cu92.cmake
 create mode 100755 tools/staticbuild/build_lib_cmake.sh

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a1aef6635119..eff4acc6ef22 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -116,6 +116,14 @@ else()
   execute_process(COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE SYSTEM_ARCHITECTURE)
 endif()
 
+if(CMAKE_BUILD_TYPE STREQUAL "Distribution" AND UNIX AND NOT APPLE)
+  set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
+  set(CMAKE_INSTALL_RPATH $\{ORIGIN\})
+  # Enforce DT_PATH instead of DT_RUNPATH
+  set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--disable-new-dtags")
+  set(CMAKE_EXE_LINKER_FLAGS "-Wl,--disable-new-dtags")
+endif()
+
 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules;${CMAKE_MODULE_PATH}")
 
 SET(EXTRA_OPERATORS "" CACHE PATH "EXTRA OPERATORS PATH")
@@ -435,11 +443,11 @@ if(USE_OPENMP)
     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/openmp)
   endfunction()
 
-  find_package(OpenMP REQUIRED)
   # This should build on Windows, but there's some problem and I don't have a Windows box, so
   # could a Windows user please fix?
   if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/openmp/CMakeLists.txt
      AND SYSTEM_ARCHITECTURE STREQUAL "x86_64"
+     AND NOT CMAKE_BUILD_TYPE STREQUAL "Distribution"
      AND NOT MSVC
      AND NOT CMAKE_CROSSCOMPILING)
     load_omp()
@@ -452,6 +460,7 @@ if(USE_OPENMP)
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
     add_definitions(-DMXNET_USE_OPENMP=1)
   else()
+    find_package(OpenMP REQUIRED)
     if(OPENMP_FOUND)
       set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
@@ -469,7 +478,9 @@ endif()
 if(USE_LAPACK)
   message("USE_LAPACK is ON")
   add_definitions(-DMXNET_USE_LAPACK=1)
-  if (NOT MSVC)
+  # In the CMAKE_BUILD_TYPE="Distribution" case, we link against Blas libraries
+  # that already provide lapack symbols. Thus -llapack would be wrong.
+  if (NOT MSVC AND NOT CMAKE_BUILD_TYPE STREQUAL "Distribution")
     list(APPEND mxnet_LINKER_LIBS lapack)
   endif()
 endif()
diff --git a/ci/docker/install/ubuntu_publish.sh b/ci/docker/install/ubuntu_publish.sh
index 6bc5a8ceb331..306a1a3143fd 100755
--- a/ci/docker/install/ubuntu_publish.sh
+++ b/ci/docker/install/ubuntu_publish.sh
@@ -27,6 +27,7 @@ add-apt-repository ppa:openjdk-r/ppa -y # Java lib
 apt-get update
 apt-get install -y git \
     cmake3 \
+    ninja-build \
     libcurl4-openssl-dev \
     unzip \
     gcc-4.8 \
@@ -57,6 +58,18 @@ mv apache-maven-3.3.9/ /usr/local/maven/
 update-alternatives --install /usr/bin/mvn mvn /usr/local/maven/apache-maven-3.3.9/bin/mvn 1
 update-ca-certificates -f
 
+# patchelf available starting Ubuntu 16.04; compile from source for Ubuntu 14.04
+mkdir /usr/local/patchelf
+cd /usr/local/patchelf
+curl -L -o patchelf-0.10.tar.gz https://github.com/NixOS/patchelf/archive/0.10.tar.gz
+tar xzf patchelf-0.10.tar.gz
+cd /usr/local/patchelf/patchelf-0.10
+./bootstrap.sh
+./configure
+make
+sudo make install
+cd /
+
 apt-get install -y python python-pip python3 python3-pip
 
 # the version of the pip shipped with ubuntu may be too lower, install a recent version here
@@ -64,4 +77,12 @@ apt-get install -y python python-pip python3 python3-pip
 python3 -m pip install --upgrade 'pip<19'
 
 # Restrict numpy version to <1.18 due to use of Python 3.4 on Ubuntu 14.04
-python3 -m pip install --upgrade --ignore-installed nose cpplint==1.3.0 pylint==2.3.1 'numpy>1.16.0,<1.18' nose-timer 'requests<2.19.0,>=2.18.4' h5py==2.8.0rc1 scipy==1.0.1 boto3
\ No newline at end of file
+python3 -m pip install --upgrade --ignore-installed nose cpplint==1.3.0 pylint==2.3.1 'numpy>1.16.0,<1.18' nose-timer 'requests<2.19.0,>=2.18.4' h5py==2.8.0rc1 scipy==1.0.1 boto3
+
+# CMake 3.13.2+ is required
+mkdir /opt/cmake && cd /opt/cmake
+wget -nv https://cmake.org/files/v3.13/cmake-3.13.5-Linux-x86_64.sh
+sh cmake-3.13.5-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
+ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
+rm cmake-3.13.5-Linux-x86_64.sh
+cmake --version
diff --git a/ci/docker/runtime_functions.sh b/ci/docker/runtime_functions.sh
index 3baa2cfb9acc..e0be19e33214 100755
--- a/ci/docker/runtime_functions.sh
+++ b/ci/docker/runtime_functions.sh
@@ -2005,6 +2005,24 @@ build_static_python_cu101mkl() {
     popd
 }
 
+build_static_python_cpu_cmake() {
+    set -ex
+    pushd .
+    export mxnet_variant=cpu
+    export CMAKE_STATICBUILD=1
+    ./ci/publish/python/build.sh
+    popd
+}
+
+build_static_python_cu101_cmake() {
+    set -ex
+    pushd .
+    export mxnet_variant=cu101
+    export CMAKE_STATICBUILD=1
+    ./ci/publish/python/build.sh
+    popd
+}
+
 publish_scala_build() {
     set -ex
     pushd .
diff --git a/ci/jenkins/Jenkins_steps.groovy b/ci/jenkins/Jenkins_steps.groovy
index 61fae5259faa..7a37826ed981 100644
--- a/ci/jenkins/Jenkins_steps.groovy
+++ b/ci/jenkins/Jenkins_steps.groovy
@@ -663,6 +663,19 @@ def test_static_python_cpu() {
   }]
 }
 
+def test_static_python_cpu_cmake() {
+    return ['Static build CPU 14.04 Python with CMake' : {
+        node(NODE_LINUX_CPU) {
+            ws('workspace/ut-publish-python-cpu') {
+                timeout(time: max_time, unit: 'MINUTES') {
+                    utils.init_git()
+                    utils.docker_run("publish.ubuntu1404_cpu", 'build_static_python_cpu_cmake', false)
+                }
+            }
+        }
+    }]
+}
+
 def test_static_python_gpu() {
   return ['Static build GPU 14.04 Python' : {
     node(NODE_LINUX_GPU) {
@@ -676,6 +689,19 @@ def test_static_python_gpu() {
   }]
 }
 
+def test_static_python_gpu_cmake() {
+    return ['Static build GPU 14.04 Python' : {
+        node(NODE_LINUX_GPU) {
+            ws('workspace/ut-publish-python-gpu') {
+                timeout(time: max_time, unit: 'MINUTES') {
+                    utils.init_git()
+                    utils.docker_run("publish.ubuntu1404_gpu", 'build_static_python_cu101_cmake', true)
+                }
+            }
+        }
+    }]
+}
+
 def test_unix_python3_cpu() {
     return ['Python3: CPU': {
       node(NODE_LINUX_CPU) {
diff --git a/ci/jenkins/Jenkinsfile_unix_cpu b/ci/jenkins/Jenkinsfile_unix_cpu
index 0b6ad661a3eb..71917de58e82 100644
--- a/ci/jenkins/Jenkinsfile_unix_cpu
+++ b/ci/jenkins/Jenkinsfile_unix_cpu
@@ -62,6 +62,7 @@ core_logic: {
     custom_steps.test_unix_cpp_cpu(),
     custom_steps.test_static_scala_cpu(),
     custom_steps.test_static_python_cpu(),
+    custom_steps.test_static_python_cpu_cmake(),
     /*  Disabled due to master build failure:
      *  http://jenkins.mxnet-ci.amazon-ml.com/blue/organizations/jenkins/incubator-mxnet/detail/master/1221/pipeline/
      *  https://github.com/apache/incubator-mxnet/issues/11801
diff --git a/ci/jenkins/Jenkinsfile_unix_gpu b/ci/jenkins/Jenkinsfile_unix_gpu
index 8c07ee89798a..f3ed638f21fa 100644
--- a/ci/jenkins/Jenkinsfile_unix_gpu
+++ b/ci/jenkins/Jenkinsfile_unix_gpu
@@ -61,6 +61,7 @@ core_logic: {
     custom_steps.test_unix_scala_gpu(),
     custom_steps.test_unix_distributed_kvstore_gpu(),
     custom_steps.test_static_python_gpu(),
+    custom_steps.test_static_python_gpu_cmake(),
     custom_steps.test_unix_python3_gpu_no_tvm_op(),
     custom_steps.test_unix_capi_cpp_package(),
 
diff --git a/config/distribution/linux_cpu.cmake b/config/distribution/linux_cpu.cmake
new file mode 100644
index 000000000000..6cdd6f127da1
--- /dev/null
+++ b/config/distribution/linux_cpu.cmake
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA OFF CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
diff --git a/config/distribution/linux_cu100.cmake b/config/distribution/linux_cu100.cmake
new file mode 100644
index 000000000000..a176caa46e3b
--- /dev/null
+++ b/config/distribution/linux_cu100.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-10.0/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.5" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu101.cmake b/config/distribution/linux_cu101.cmake
new file mode 100644
index 000000000000..082d8144b537
--- /dev/null
+++ b/config/distribution/linux_cu101.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-10.1/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.5" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu102.cmake b/config/distribution/linux_cu102.cmake
new file mode 100644
index 000000000000..9d165338cd29
--- /dev/null
+++ b/config/distribution/linux_cu102.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-10.2/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.5" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu75.cmake b/config/distribution/linux_cu75.cmake
new file mode 100644
index 000000000000..a1ad234f6fab
--- /dev/null
+++ b/config/distribution/linux_cu75.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-7.5/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;3.5;5.0;5.2" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu80.cmake b/config/distribution/linux_cu80.cmake
new file mode 100644
index 000000000000..b15e4a0f2977
--- /dev/null
+++ b/config/distribution/linux_cu80.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-8.0/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;6.2" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu90.cmake b/config/distribution/linux_cu90.cmake
new file mode 100644
index 000000000000..29d571f69e9b
--- /dev/null
+++ b/config/distribution/linux_cu90.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-9.0/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.2" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu91.cmake b/config/distribution/linux_cu91.cmake
new file mode 100644
index 000000000000..42dd158be3bc
--- /dev/null
+++ b/config/distribution/linux_cu91.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-9.1/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.2" CACHE STRING "Cuda architectures")
diff --git a/config/distribution/linux_cu92.cmake b/config/distribution/linux_cu92.cmake
new file mode 100644
index 000000000000..04776de4480c
--- /dev/null
+++ b/config/distribution/linux_cu92.cmake
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+
+set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
+set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
+set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
+set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
+set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
+set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
+set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
+set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
+set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
+
+set(CUDACXX "/usr/local/cuda-9.2/bin/nvcc" CACHE STRING "Cuda compiler")
+set(MXNET_CUDA_ARCH "3.0;5.0;6.0;7.0;7.2" CACHE STRING "Cuda architectures")
diff --git a/tools/dependencies/openblas.sh b/tools/dependencies/openblas.sh
index 05a500da3147..f3283c97b605 100755
--- a/tools/dependencies/openblas.sh
+++ b/tools/dependencies/openblas.sh
@@ -20,7 +20,8 @@
 # This script builds the static library of openblas that can be used as dependency of mxnet.
 set -ex
 OPENBLAS_VERSION=0.3.7
-if [[ ! -e $DEPS_PATH/lib/libopenblas.a ]]; then
+if [[ ((! -e $DEPS_PATH/lib/libopenblas.a) && -z "$CMAKE_STATICBUILD") ||
+          ((! -e $DEPS_PATH/lib/libopenblas.so) && -v CMAKE_STATICBUILD) ]]; then
     # download and build openblas
     >&2 echo "Building openblas..."
 
@@ -33,12 +34,24 @@ if [[ ! -e $DEPS_PATH/lib/libopenblas.a ]]; then
 
     # Adding NO_DYNAMIC=1 flag causes make install to fail
     $MAKE DYNAMIC_ARCH=1 USE_OPENMP=1
+
+    if [[ -v CMAKE_STATICBUILD ]]; then
+        # We link and redistribute libopenblas.so for cmake staticbuild
+        # cf https://gitlab.kitware.com/cmake/cmake/issues/16221#note_143330
+        patchelf --set-rpath '$ORIGIN' --force-rpath libopenblas.so
+    fi
+
     $MAKE PREFIX=$DEPS_PATH install
 
-    # Manually removing .so to avoid linking against it
-    rm $DEPS_PATH/lib/libopenblasp-r${OPENBLAS_VERSION}.so
+
+    if [[ -z "$CMAKE_STATICBUILD" ]]; then
+        # Manually removing .so to avoid linking against it
+        rm $DEPS_PATH/lib/libopenblasp-r${OPENBLAS_VERSION}.so
+    fi
 
     popd
-    ln -s libopenblas.a $DEPS_PATH/lib/libcblas.a
-    ln -s libopenblas.a $DEPS_PATH/lib/liblapack.a
+    if [[ -z "$CMAKE_STATICBUILD" ]]; then
+        ln -s libopenblas.a $DEPS_PATH/lib/libcblas.a
+        ln -s libopenblas.a $DEPS_PATH/lib/liblapack.a
+    fi
 fi
diff --git a/tools/dependencies/opencv.sh b/tools/dependencies/opencv.sh
index 4b85f018b0d3..eb4730c505ed 100755
--- a/tools/dependencies/opencv.sh
+++ b/tools/dependencies/opencv.sh
@@ -161,7 +161,7 @@ if [[ ! -f $DEPS_PATH/lib/libopencv_core.a ]] || [[ ! -f $DEPS_PATH/lib/libopenc
           -D BUILD_opencv_gpuoptflow=OFF \
           -D BUILD_opencv_gpustereo=OFF \
           -D BUILD_opencv_gpuwarping=OFF \
-          -D BUILD_opencv_highgui=OFF \
+          -D BUILD_opencv_highgui=ON \
           -D BUILD_opencv_java=OFF \
           -D BUILD_opencv_js=OFF \
           -D BUILD_opencv_ml=OFF \
diff --git a/tools/pip/setup.py b/tools/pip/setup.py
index 36934a80761b..a0b63cc866f9 100644
--- a/tools/pip/setup.py
+++ b/tools/pip/setup.py
@@ -154,10 +154,18 @@ def has_ext_modules(self):
         shutil.copytree(os.path.join(CURRENT_DIR, 'mxnet-build/3rdparty/mkldnn/build/install/include'),
                     os.path.join(CURRENT_DIR, 'mxnet/include/mkldnn'))
 if platform.system() == 'Linux':
-    shutil.copy(os.path.join(os.path.dirname(LIB_PATH[0]), 'libgfortran.so.3'), os.path.join(CURRENT_DIR, 'mxnet'))
-    package_data['mxnet'].append('mxnet/libgfortran.so.3')
-    shutil.copy(os.path.join(os.path.dirname(LIB_PATH[0]), 'libquadmath.so.0'), os.path.join(CURRENT_DIR, 'mxnet'))
+    libdir, mxdir = os.path.dirname(LIB_PATH[0]), os.path.join(CURRENT_DIR, 'mxnet')
+    if os.path.exists(os.path.join(libdir, 'libgfortran.so.3')):
+        shutil.copy(os.path.join(libdir, 'libgfortran.so.3'), mxdir)
+        package_data['mxnet'].append('mxnet/libgfortran.so.4')
+    else:
+        shutil.copy(os.path.join(libdir, 'libgfortran.so.4'), mxdir)
+        package_data['mxnet'].append('mxnet/libgfortran.so.4')
+    shutil.copy(os.path.join(libdir, 'libquadmath.so.0'), mxdir)
     package_data['mxnet'].append('mxnet/libquadmath.so.0')
+    if os.path.exists(os.path.join(libdir, 'libopenblas.so.0')):
+        shutil.copy(os.path.join(libdir, 'libopenblas.so.0'), mxdir)
+        package_data['mxnet'].append('mxnet/libquadmath.so.0')
 
 # Copy licenses and notice
 for f in os.listdir('mxnet/licenses'):
diff --git a/tools/staticbuild/README.md b/tools/staticbuild/README.md
index b48043b1ef87..201bf76276f5 100644
--- a/tools/staticbuild/README.md
+++ b/tools/staticbuild/README.md
@@ -20,16 +20,31 @@
 This folder contains the core script used to build the static library. This README provides information on how to use the scripts in this folder. Please be aware, all of the scripts are designed to be run under the root folder.
 
 ## `build.sh`
-This script is a wrapper around `build_lib.sh. It simplifies the things by automatically identifing the system version, number of cores, and all environment variable settings. Here are examples you can run with this script:
+This script is a wrapper around `build_lib.sh. It simplifies the build by
+automatically identifing the system version, number of cores, and all
+environment variable settings. Here are examples you can run with this script:
 
 ```
-tools/staticbuild/build.sh cu92
+tools/staticbuild/build.sh cu102
 ```
-This would build the mxnet package based on CUDA9.2.
+
+This would build the mxnet package based on CUDA 10.2.
+
+```
+tools/staticbuild/build.sh cpu
+```
+
+This would build the mxnet package based on MKLDNN.
+
+To use CMake to build the `libmxnet.so` instead of the deprecated Makefile based
+build logic, set the `CMAKE_STATICBUILD` environment variable. For example
+
 ```
-tools/staticbuild/build.sh mkl
+CMAKE_STATICBUILD=1 tools/staticbuild/build.sh cpu
 ```
-This would build the mxnet package based on MKLDNN and and pypi configuration settings.
+
+For the CMake build, you need to install `patchelf` first, for example via `apt
+install patchelf` on Ubuntu systems.
 
 As the result, users would have a complete static dependencies in `/staticdeps` in the root folder as well as a static-linked `libmxnet.so` file lives in `lib`. You can build your language binding by using the `libmxnet.so`.
 
diff --git a/tools/staticbuild/build.sh b/tools/staticbuild/build.sh
index ad08e63483b8..b43e35fc6c3b 100755
--- a/tools/staticbuild/build.sh
+++ b/tools/staticbuild/build.sh
@@ -72,4 +72,8 @@ cp DISCLAIMER-WIP licenses/
 
 
 # Build mxnet
-source tools/staticbuild/build_lib.sh
+if [[ -z "$CMAKE_STATICBUILD" ]]; then
+    source tools/staticbuild/build_lib.sh
+else
+    source tools/staticbuild/build_lib_cmake.sh
+fi
diff --git a/tools/staticbuild/build_lib.sh b/tools/staticbuild/build_lib.sh
index f4fbf399ce86..111c907db863 100755
--- a/tools/staticbuild/build_lib.sh
+++ b/tools/staticbuild/build_lib.sh
@@ -17,6 +17,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
+set -eo pipefail
+
 # This script builds the libraries of mxnet.
 make_config=make/staticbuild/${PLATFORM}_${VARIANT}.mk
 if [[ ! -f $make_config ]]; then
@@ -41,7 +43,13 @@ fi
 $MAKE DEPS_PATH=$DEPS_PATH
 
 if [[ $PLATFORM == 'linux' ]]; then
-    cp -L /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.so lib/libgfortran.so.3
+    if [[ -f /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.so ]]; then
+        cp -L /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.so lib/libgfortran.so.3
+    elif [[ -f /usr/lib/x86_64-linux-gnu/libgfortran.so.3 ]]; then
+        cp -L /usr/lib/x86_64-linux-gnu/libgfortran.so.3 lib/libgfortran.so.3
+    else
+        cp -L /usr/lib/x86_64-linux-gnu/libgfortran.so.4 lib/libgfortran.so.4
+    fi
     cp -L /usr/lib/x86_64-linux-gnu/libquadmath.so.0 lib/libquadmath.so.0
 fi
 
@@ -57,4 +65,6 @@ else
     >&2 echo "Not available"
 fi
 
-ln -s staticdeps/ deps
+if [[ ! -L deps ]]; then
+    ln -s staticdeps deps
+fi
diff --git a/tools/staticbuild/build_lib_cmake.sh b/tools/staticbuild/build_lib_cmake.sh
new file mode 100755
index 000000000000..78718eab5e67
--- /dev/null
+++ b/tools/staticbuild/build_lib_cmake.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set -eo pipefail
+
+# This script builds the libraries of mxnet.
+cmake_config=${CURDIR}/config/distribution/${PLATFORM}_${VARIANT}.cmake
+if [[ ! -f $cmake_config ]]; then
+    >&2 echo "Couldn't find cmake config $make_config for the current settings."
+    exit 1
+fi
+
+git submodule update --init --recursive || true
+
+# Build libmxnet.so
+rm -rf build; mkdir build; cd build
+cmake -GNinja -C $cmake_config -DCMAKE_PREFIX_PATH=${DEPS_PATH} -DCMAKE_FIND_ROOT_PATH=${DEPS_PATH} ..
+ninja
+cd -
+
+# Move to lib
+rm -rf lib; mkdir lib; cp -L build/libmxnet.so lib/libmxnet.so
+
+if [[ $PLATFORM == 'linux' ]]; then
+    cp -L staticdeps/lib/libopenblas.so lib/libopenblas.so.0
+    if [[ -f /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.so ]]; then
+        cp -L /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.so lib/libgfortran.so.3
+    elif [[ -f /usr/lib/x86_64-linux-gnu/libgfortran.so.3 ]]; then
+        cp -L /usr/lib/x86_64-linux-gnu/libgfortran.so.3 lib/libgfortran.so.3
+    else
+        cp -L /usr/lib/x86_64-linux-gnu/libgfortran.so.4 lib/libgfortran.so.4
+    fi
+    cp -L /usr/lib/x86_64-linux-gnu/libquadmath.so.0 lib/libquadmath.so.0
+fi
+
+# Print the linked objects on libmxnet.so
+>&2 echo "Checking linked objects on libmxnet.so..."
+if [[ ! -z $(command -v readelf) ]]; then
+    readelf -d lib/libmxnet.so
+    strip --strip-unneeded lib/libmxnet.so
+elif [[ ! -z $(command -v otool) ]]; then
+    otool -L lib/libmxnet.so
+    strip -u -r -x lib/libmxnet.so
+else
+    >&2 echo "Not available"
+fi
+
+if [[ ! -L deps ]]; then
+    ln -s staticdeps deps
+fi

From 6b0c404c436b5b7996fa92e59b902b8f07f31651 Mon Sep 17 00:00:00 2001
From: Leonard Lausen <lausen@amazon.com>
Date: Thu, 6 Feb 2020 23:46:27 +0000
Subject: [PATCH 2/3] Set USE_SSE ON

---
 config/distribution/linux_cpu.cmake   | 2 +-
 config/distribution/linux_cu100.cmake | 2 +-
 config/distribution/linux_cu101.cmake | 2 +-
 config/distribution/linux_cu102.cmake | 2 +-
 config/distribution/linux_cu75.cmake  | 2 +-
 config/distribution/linux_cu80.cmake  | 2 +-
 config/distribution/linux_cu90.cmake  | 2 +-
 config/distribution/linux_cu91.cmake  | 2 +-
 config/distribution/linux_cu92.cmake  | 2 +-
 9 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/config/distribution/linux_cpu.cmake b/config/distribution/linux_cpu.cmake
index 6cdd6f127da1..43de52e97a74 100644
--- a/config/distribution/linux_cpu.cmake
+++ b/config/distribution/linux_cpu.cmake
@@ -24,5 +24,5 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
diff --git a/config/distribution/linux_cu100.cmake b/config/distribution/linux_cu100.cmake
index a176caa46e3b..a48c30fcd2f7 100644
--- a/config/distribution/linux_cu100.cmake
+++ b/config/distribution/linux_cu100.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-10.0/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu101.cmake b/config/distribution/linux_cu101.cmake
index 082d8144b537..5a832281a09a 100644
--- a/config/distribution/linux_cu101.cmake
+++ b/config/distribution/linux_cu101.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-10.1/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu102.cmake b/config/distribution/linux_cu102.cmake
index 9d165338cd29..ce5c46e7d5a0 100644
--- a/config/distribution/linux_cu102.cmake
+++ b/config/distribution/linux_cu102.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-10.2/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu75.cmake b/config/distribution/linux_cu75.cmake
index a1ad234f6fab..fcda8cb41550 100644
--- a/config/distribution/linux_cu75.cmake
+++ b/config/distribution/linux_cu75.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-7.5/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu80.cmake b/config/distribution/linux_cu80.cmake
index b15e4a0f2977..b38a5762507c 100644
--- a/config/distribution/linux_cu80.cmake
+++ b/config/distribution/linux_cu80.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-8.0/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu90.cmake b/config/distribution/linux_cu90.cmake
index 29d571f69e9b..2b970e1fe6c1 100644
--- a/config/distribution/linux_cu90.cmake
+++ b/config/distribution/linux_cu90.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-9.0/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu91.cmake b/config/distribution/linux_cu91.cmake
index 42dd158be3bc..5d6a1487034f 100644
--- a/config/distribution/linux_cu91.cmake
+++ b/config/distribution/linux_cu91.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-9.1/bin/nvcc" CACHE STRING "Cuda compiler")
diff --git a/config/distribution/linux_cu92.cmake b/config/distribution/linux_cu92.cmake
index 04776de4480c..3fc50e5a7ed4 100644
--- a/config/distribution/linux_cu92.cmake
+++ b/config/distribution/linux_cu92.cmake
@@ -25,7 +25,7 @@ set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
 set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
 set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
 set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
-set(USE_SSE OFF CACHE BOOL "Build with x86 SSE instruction support")
+set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
 set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
 
 set(CUDACXX "/usr/local/cuda-9.2/bin/nvcc" CACHE STRING "Cuda compiler")

From 94f09a92e2eb9b0f3554a2616fb6879df2776b1a Mon Sep 17 00:00:00 2001
From: Leonard Lausen <lausen@amazon.com>
Date: Thu, 6 Feb 2020 23:57:46 +0000
Subject: [PATCH 3/3] Enforce instruction set

---
 config/distribution/linux_cpu.cmake   | 2 ++
 config/distribution/linux_cu100.cmake | 2 ++
 config/distribution/linux_cu101.cmake | 4 ++++
 config/distribution/linux_cu102.cmake | 2 ++
 config/distribution/linux_cu75.cmake  | 2 ++
 config/distribution/linux_cu80.cmake  | 2 ++
 config/distribution/linux_cu90.cmake  | 2 ++
 config/distribution/linux_cu91.cmake  | 2 ++
 config/distribution/linux_cu92.cmake  | 2 ++
 tools/dependencies/openblas.sh        | 2 +-
 tools/dependencies/opencv.sh          | 2 +-
 tools/staticbuild/build.sh            | 4 ++--
 12 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/config/distribution/linux_cpu.cmake b/config/distribution/linux_cpu.cmake
index 43de52e97a74..cad348578454 100644
--- a/config/distribution/linux_cpu.cmake
+++ b/config/distribution/linux_cpu.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA OFF CACHE BOOL "Build with CUDA support")
 set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
diff --git a/config/distribution/linux_cu100.cmake b/config/distribution/linux_cu100.cmake
index a48c30fcd2f7..d26b4d73eee7 100644
--- a/config/distribution/linux_cu100.cmake
+++ b/config/distribution/linux_cu100.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu101.cmake b/config/distribution/linux_cu101.cmake
index 5a832281a09a..aaf76cc10df1 100644
--- a/config/distribution/linux_cu101.cmake
+++ b/config/distribution/linux_cu101.cmake
@@ -16,6 +16,10 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu102.cmake b/config/distribution/linux_cu102.cmake
index ce5c46e7d5a0..6b575683e919 100644
--- a/config/distribution/linux_cu102.cmake
+++ b/config/distribution/linux_cu102.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu75.cmake b/config/distribution/linux_cu75.cmake
index fcda8cb41550..45ba2b9de5d7 100644
--- a/config/distribution/linux_cu75.cmake
+++ b/config/distribution/linux_cu75.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu80.cmake b/config/distribution/linux_cu80.cmake
index b38a5762507c..ce8e0083bcad 100644
--- a/config/distribution/linux_cu80.cmake
+++ b/config/distribution/linux_cu80.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu90.cmake b/config/distribution/linux_cu90.cmake
index 2b970e1fe6c1..01097cb882e4 100644
--- a/config/distribution/linux_cu90.cmake
+++ b/config/distribution/linux_cu90.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu91.cmake b/config/distribution/linux_cu91.cmake
index 5d6a1487034f..f6301fa9f720 100644
--- a/config/distribution/linux_cu91.cmake
+++ b/config/distribution/linux_cu91.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/config/distribution/linux_cu92.cmake b/config/distribution/linux_cu92.cmake
index 3fc50e5a7ed4..63ab9fce20d8 100644
--- a/config/distribution/linux_cu92.cmake
+++ b/config/distribution/linux_cu92.cmake
@@ -16,6 +16,8 @@
 # under the License.
 
 set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
+set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
+set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
 
 set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
 set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
diff --git a/tools/dependencies/openblas.sh b/tools/dependencies/openblas.sh
index f3283c97b605..cdc63b6a8355 100755
--- a/tools/dependencies/openblas.sh
+++ b/tools/dependencies/openblas.sh
@@ -33,7 +33,7 @@ if [[ ((! -e $DEPS_PATH/lib/libopenblas.a) && -z "$CMAKE_STATICBUILD") ||
     cd $DEPS_PATH/OpenBLAS-$OPENBLAS_VERSION
 
     # Adding NO_DYNAMIC=1 flag causes make install to fail
-    $MAKE DYNAMIC_ARCH=1 USE_OPENMP=1
+    CXX="g++ -fPIC" CC="gcc -fPIC" $MAKE DYNAMIC_ARCH=1 USE_OPENMP=1
 
     if [[ -v CMAKE_STATICBUILD ]]; then
         # We link and redistribute libopenblas.so for cmake staticbuild
diff --git a/tools/dependencies/opencv.sh b/tools/dependencies/opencv.sh
index eb4730c505ed..139b68c5bef0 100755
--- a/tools/dependencies/opencv.sh
+++ b/tools/dependencies/opencv.sh
@@ -46,7 +46,7 @@ if [[ ! -f $DEPS_PATH/lib/libopencv_core.a ]] || [[ ! -f $DEPS_PATH/lib/libopenc
     mkdir -p $DEPS_PATH/opencv-$OPENCV_VERSION/build
     pushd .
     cd $DEPS_PATH/opencv-$OPENCV_VERSION/build
-    cmake \
+    CXX="g++ -fPIC" CC="gcc -fPIC" cmake \
           -D OPENCV_ENABLE_NONFREE=OFF \
           -D WITH_1394=OFF \
           -D WITH_ARAVIS=OFF \
diff --git a/tools/staticbuild/build.sh b/tools/staticbuild/build.sh
index b43e35fc6c3b..f33ce9d711bc 100755
--- a/tools/staticbuild/build.sh
+++ b/tools/staticbuild/build.sh
@@ -48,8 +48,8 @@ else
 fi
 export MAKE="make $ADD_MAKE_FLAG"
 
-export CC="gcc -fPIC"
-export CXX="g++ -fPIC"
+export CC="gcc -fPIC -mno-avx"
+export CXX="g++ -fPIC -mno-avx"
 export FC="gfortran"
 export PKG_CONFIG_PATH=$DEPS_PATH/lib/pkgconfig:$DEPS_PATH/lib64/pkgconfig:$DEPS_PATH/lib/x86_64-linux-gnu/pkgconfig:$PKG_CONFIG_PATH
 export CPATH=$DEPS_PATH/include:$CPATH
