This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
julia: migrate build process to cmake #16125
Open
iblislin
wants to merge
14
commits into
master
Choose a base branch
from
ib/jl-cmake-build
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
82a8b6e
julia: migrate build process to cmake
iblislin 6f27f0e
build with Julia's private libopenblas
iblislin 8c73cd9
submodule update
iblislin 3ef9cf0
USE_JULIA_BLAS
iblislin 27f996d
julia: migrate build process to cmake
iblislin 46e7bd2
build with Julia's private libopenblas
iblislin cede75b
submodule update
iblislin b28b847
USE_JULIA_BLAS
iblislin 4a2a9c9
Merge branch 'ib/jl-cmake-build' of github.com:apache/incubator-mxnet…
iblislin 49fad87
submodule update
iblislin e5b4871
OPENBLAS_INTERFACE64
iblislin 8779154
fix command quote warning
iblislin ef7a75e
Update julia/deps/build.jl
iblislin afe7bee
Update julia/deps/build.jl
iblislin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
using CMake | ||
using JSON | ||
using Libdl | ||
using LinearAlgebra | ||
|
@@ -93,19 +94,19 @@ else | |
end | ||
|
||
# propagate more build flags from ENV | ||
const CC = get(ENV, "CC", nothing) | ||
const CXX = get(ENV, "CXX", nothing) | ||
const ADD_CFLAGS = get(ENV, "ADD_CFLAGS", nothing) | ||
const ADD_LDFLAGS = get(ENV, "ADD_LDFLAGS", nothing) | ||
const USE_JEMALLOC = get(ENV, "USE_JEMALLOC", nothing) # "0" or "1" | ||
const USE_JEMALLOC = get(ENV, "USE_JEMALLOC", nothing) # "ON" or "OFF" | ||
|
||
function get_cpucore() | ||
if haskey(ENV, "TRAVIS") # on travis-ci | ||
2 | ||
else | ||
min(Sys.CPU_THREADS, 32) | ||
end | ||
end | ||
get_cpucore() = min(Sys.CPU_THREADS, 32) | ||
|
||
cmake_bool(x::Bool) = ifelse(x ≡ true, "ON", "OFF") | ||
|
||
cmake_jemalloc(::Nothing) = "" | ||
cmake_jemalloc(x::Bool) = "-DUSE_JEMALLOC=" * cmake_bool(x) | ||
|
||
cmake_cuda_path(::Nothing) = "" | ||
cmake_cuda_path(x::String) = "-DUSE_CUDA_PATH=" * x | ||
|
||
cmake_jl_blas(x::Bool, blas_path) = ifelse(x, "-DOpenBLAS_LIB=$blas_path", "") | ||
|
||
using BinDeps | ||
@BinDeps.setup | ||
|
@@ -137,7 +138,7 @@ if !libmxnet_detected | |
|
||
run(download_cmd(package_url, "mxnet.7z")) | ||
# this command will create the dir "usr\\lib" | ||
run(`$exe7z e mxnet.7z *\\build\\* *\\lib\\* -y -ousr\\lib`) | ||
run(`$exe7z e mxnet.7z "*\\build\\*" "*\\lib\\*" -y -ousr\\lib`) | ||
|
||
run(download_cmd(base_url, "mxnet_base.7z")) | ||
run(`$exe7z x mxnet_base.7z -y -ousr`) | ||
|
@@ -146,7 +147,7 @@ if !libmxnet_detected | |
# testing | ||
run(`cmd /c dir "usr\\lib"`) | ||
return | ||
end | ||
end # if Sys.iswindows() | ||
|
||
################################################################################ | ||
# If not found, try to build automatically using BinDeps | ||
|
@@ -155,131 +156,66 @@ if !libmxnet_detected | |
blas_path = Libdl.dlpath(Libdl.dlopen(Base.libblas_name)) | ||
blas_vendor = LinearAlgebra.BLAS.vendor() | ||
|
||
ilp64 = "" | ||
if blas_vendor == :openblas64 | ||
ilp64 = "-DINTERFACE64" | ||
end | ||
USE_JULIA_BLAS = (blas_vendor in (:openblas, :openblas64)) | ||
@info "USE_JULIA_BLAS -> $USE_JULIA_BLAS" | ||
|
||
FORCE_LAPACK = false | ||
if blas_vendor == :unknown | ||
@info("Julia is built with an unkown blas library ($blas_path).") | ||
@info("Attempting build without reusing the blas library") | ||
USE_JULIA_BLAS = false | ||
elseif !(blas_vendor in (:openblas, :openblas64)) | ||
@info("Unsure if we can build against $blas_vendor.") | ||
@info("Attempting build anyway.") | ||
USE_JULIA_BLAS = true | ||
else | ||
USE_JULIA_BLAS = true | ||
FORCE_LAPACK = true | ||
end | ||
@info("USE_JULIA_BLAS -> $USE_JULIA_BLAS") | ||
|
||
blas_name = blas_vendor == :openblas64 ? "openblas" : string(blas_vendor) | ||
MSHADOW_LDFLAGS = "MSHADOW_LDFLAGS=-lm $blas_path" | ||
blas_name = occursin("openblas", string(blas_vendor)) ? "open" : string(blas_vendor) | ||
|
||
#-------------------------------------------------------------------------------- | ||
# Build libmxnet | ||
mxnet = library_dependency("mxnet", aliases=["mxnet", "libmxnet", "libmxnet.so"]) | ||
|
||
_prefix = joinpath(BinDeps.depsdir(mxnet), "usr") | ||
_blddir = joinpath(BinDeps.depsdir(mxnet), "build") | ||
_srcdir = joinpath(BinDeps.depsdir(mxnet), "src") | ||
_mxdir = joinpath(_srcdir, "mxnet") | ||
_libdir = joinpath(_prefix, "lib") | ||
# We have do eagerly delete the installed libmxnet.so | ||
|
||
# We have to eagerly delete the build directory. | ||
# Otherwise we won't rebuild on an update. | ||
run(`rm -f $_libdir/libmxnet.$(Libdl.dlext)`) | ||
rm(_blddir, recursive=true, force=true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously it deleted the file libmxnet.* in the libdir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, the final result is the same -- making a fresh build, not increamental build. This build script is simliar to Python's setup.py. |
||
|
||
@debug "build dir -> $_blddir" | ||
|
||
provides(BuildProcess, | ||
(@build_steps begin | ||
CreateDirectory(_blddir) | ||
CreateDirectory(_srcdir) | ||
CreateDirectory(_libdir) | ||
@build_steps begin | ||
BinDeps.DirectoryRule(_mxdir, @build_steps begin | ||
ChangeDirectory(_srcdir) | ||
`git clone https://github.com/apache/incubator-mxnet mxnet` | ||
`git clone --recursive https://github.com/apache/incubator-mxnet mxnet` | ||
end) | ||
@build_steps begin | ||
ChangeDirectory(_mxdir) | ||
`git fetch` | ||
`git submodule update --recursive --force` | ||
if libmxnet_curr_ver != "master" | ||
`git checkout $libmxnet_curr_ver` | ||
else | ||
`git checkout origin/$libmxnet_curr_ver` | ||
end | ||
`git submodule update --init --recursive` | ||
`git -C 3rdparty/mshadow checkout -- make/mshadow.mk` | ||
`cp -v ../../cblas.h include/cblas.h` | ||
`sed -i -s "s/MSHADOW_CFLAGS = \(.*\)/MSHADOW_CFLAGS = \1 $ilp64/" 3rdparty/mshadow/make/mshadow.mk` | ||
|
||
# Copy config.mk, always override the file | ||
if Sys.isapple() | ||
`cp make/osx.mk config.mk` | ||
else | ||
`cp make/config.mk config.mk` | ||
end | ||
|
||
# Configure OpenCV | ||
`sed -i -s 's/USE_OPENCV = 1/USE_OPENCV = 0/' config.mk` | ||
|
||
# Configure CUDA | ||
if HAS_CUDA | ||
@build_steps begin | ||
`sed -i -s 's/USE_CUDA = 0/USE_CUDA = 1/' config.mk` | ||
# address https://github.com/apache/incubator-mxnet/pull/7856 | ||
`sed -i -s "s/ADD_LDFLAGS =\(.*\)/ADD_LDFLAGS =\1 -lcublas -lcusolver -lcurand -lcudart/" config.mk` | ||
if haskey(ENV, "CUDA_HOME") | ||
`sed -i -s "s@USE_CUDA_PATH = NONE@USE_CUDA_PATH = $(ENV["CUDA_HOME"])@" config.mk` | ||
end | ||
if haskey(ENV, "CUDA_HOME") | ||
# address https://github.com/apache/incubator-mxnet/pull/7838 | ||
flag = "-L$(ENV["CUDA_HOME"])/lib64 -L$(ENV["CUDA_HOME"])/lib" | ||
`sed -i -s "s@ADD_LDFLAGS =\(.*\)@ADD_LDFLAGS =\1 $flag@" config.mk` | ||
end | ||
if HAS_CUDNN | ||
`sed -i -s 's/USE_CUDNN = 0/USE_CUDNN = 1/' config.mk` | ||
end | ||
end | ||
end | ||
|
||
# Force enable LAPACK build | ||
# Julia's OpenBLAS has LAPACK functionality already | ||
if FORCE_LAPACK | ||
if Sys.isapple() | ||
MSHADOW_LDFLAGS *= " -framework Accelerate" | ||
end | ||
`sed -i -s 's/ADD_CFLAGS =\(.*\)/ADD_CFLAGS =\1 -DMXNET_USE_LAPACK/' config.mk` | ||
end | ||
|
||
# propagate more build flags from ENV | ||
if CC != nothing | ||
`sed -i -s "s@^export CC =\(.*\)@export CC = $CC@" config.mk` | ||
end | ||
if CXX != nothing | ||
`sed -i -s "s@^export CXX =\(.*\)@export CXX = $CXX@" config.mk` | ||
end | ||
if ADD_CFLAGS != nothing | ||
`sed -i -s "s@ADD_CFLAGS =\(.*\)@ADD_CFLAGS =\1 $ADD_CFLAGS@" config.mk` | ||
end | ||
if ADD_LDFLAGS != nothing | ||
`sed -i -s "s@ADD_LDFLAGS =\(.*\)@ADD_LDFLAGS =\1 $ADD_LDFLAGS@" config.mk` | ||
end | ||
if USE_JEMALLOC != nothing | ||
`sed -i -s "s@USE_JEMALLOC =\(.*\)@USE_JEMALLOC = $USE_JEMALLOC@" config.mk` | ||
end | ||
|
||
if USE_JULIA_BLAS | ||
`make -j$(get_cpucore()) USE_BLAS=$blas_name $MSHADOW_LDFLAGS` | ||
else | ||
`make -j$(get_cpucore())` | ||
end | ||
`cp -f -v julia/deps/include/cblas.h include/cblas.h` | ||
end | ||
@build_steps begin | ||
ChangeDirectory(_blddir) | ||
`$cmake | ||
-DCMAKE_BUILD_TYPE=$(libmxnet_curr_ver == "master" ? "Debug" : "Release") | ||
-DUSE_BLAS=$blas_name | ||
-DUSE_OPENCV=$(cmake_bool(false)) | ||
-DUSE_CUDA=$(cmake_bool(HAS_CUDA)) | ||
-DUSE_CUDNN=$(cmake_bool(HAS_CUDNN)) | ||
$(cmake_jemalloc(USE_JEMALLOC)) | ||
$(cmake_cuda_path(get(ENV, "CUDA_HOME", nothing))) | ||
$(cmake_jl_blas(USE_JULIA_BLAS, blas_path)) | ||
$_mxdir` | ||
`make -j$(get_cpucore()) VERBOSE=$(Int(libmxnet_curr_ver == "master"))` | ||
end | ||
FileRule(joinpath(_libdir, "libmxnet.$(Libdl.dlext)"), @build_steps begin | ||
# the output file on macos is still in `.so` suffix | ||
# so we rename it | ||
`cp $_mxdir/lib/libmxnet.so $_libdir/libmxnet.$(Libdl.dlext)` | ||
FileRule(joinpath(_blddir, "libmxnet.$(Libdl.dlext)"), @build_steps begin | ||
# the output file on macos is still in `.so` suffix, | ||
# so we create a soft link for it. | ||
`ln -s libmxnet.so $_blddir/libmxnet.$(Libdl.dlext)` | ||
end) | ||
end | ||
end), mxnet, installed_libpath=_libdir) | ||
end), mxnet, installed_libpath=_blddir) | ||
|
||
@BinDeps.install Dict(:mxnet => :mxnet) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could anyone review this cmake script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know much, but maybe you can simplify a bit using CHECK_SOURCE_COMPILES and similar. https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-To-Write-Platform-Checks
Overall looks ok-ish to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, looks greate, I will try
CHECK_SYMBOL_EXISTS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any updates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, I'm swamped... I will try it after 1/10.