From 51185bff3a558cf53867252093498249859cc5a9 Mon Sep 17 00:00:00 2001 From: Daniel Hershcovich Date: Wed, 16 Aug 2017 17:12:33 +0300 Subject: [PATCH] Fix build happening twice in manual installation --- .travis.yml | 18 +++++---- doc/source/python.rst | 8 ++-- setup.py | 90 ++++++++++++++++++++++--------------------- 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/.travis.yml b/.travis.yml index bb36cf1c3..ad10201ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,6 +60,7 @@ install: cmake .. || travis_terminate 1; sudo make install || travis_terminate 1; cd ../..; + export EIGEN3_INCLUDE_DIR=/usr/local/include/eigen3; fi' before_script: @@ -69,7 +70,8 @@ before_script: - 'if [[ "$PYTHON_INSTALL" == "manual" ]]; then mkdir build; cd build; - cmake .. -DEIGEN3_INCLUDE_DIR=/usr/local/include/eigen3 -DENABLE_BOOST=ON -DENABLE_CPP_EXAMPLES=ON -DPYTHON=$(which python) || travis_terminate 1; + export INSTALL_PREFIX=$(dirname $(which python))/..; + cmake .. -DEIGEN3_INCLUDE_DIR=${EIGEN3_INCLUDE_DIR} -DENABLE_BOOST=ON -DENABLE_CPP_EXAMPLES=ON -DPYTHON=$(which python) -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX || travis_terminate 1; else pip install -v . || travis_terminate 1; fi' @@ -80,17 +82,17 @@ after_failure: script: - 'if [[ "$PYTHON_INSTALL" == "manual" ]]; then if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - make -j$(nproc); - fi; - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then - make -j$(sysctl -n hw.ncpu); - export DYLD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/build/dynet:$DYLD_LIBRARY_PATH; + make -j$(nproc) || travis_terminate 1; + elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then + make -j$(sysctl -n hw.ncpu) || travis_terminate 1; + export DYLD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/build/dynet; fi; + make install || travis_terminate 1; make test || travis_terminate 1; cd python; - python ../../setup.py build --build-dir=.. install --user || travis_terminate 1; + python ../../setup.py build --build-dir=.. --skip-build install --user || travis_terminate 1; elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then - export DYLD_LIBRARY_PATH=$(dirname $(which python))/../lib:$DYLD_LIBRARY_PATH; + export DYLD_LIBRARY_PATH=$INSTALL_PREFIX/lib:$DYLD_LIBRARY_PATH; fi' - cd $TRAVIS_BUILD_DIR/tests/python - python test.py diff --git a/doc/source/python.rst b/doc/source/python.rst index db8d38a28..b87b2d605 100644 --- a/doc/source/python.rst +++ b/doc/source/python.rst @@ -66,7 +66,7 @@ The following is a list of all the commands needed to perform a manual install: make -j 2 # replace 2 with the number of available cores cd python - python ../../setup.py build --build-dir=.. install # add `--user` for a user-local install. + python ../../setup.py build --build-dir=.. --skip-build install # add `--user` for a user-local install. # this should suffice, but on some systems you may need to add the following line to your # init files in order for the compiled .so files be accessible to Python. @@ -149,12 +149,12 @@ in the system, run the following: .. code:: bash cd $PATH_TO_DYNET/build/python - python ../../setup.py build --build-dir=.. install --user + python ../../setup.py build --build-dir=.. --skip-build install --user The ``--user`` switch will install the module in your local site-packages, and works without root privileges. To install the module to the system site-packages (for all users), or to the current `virtualenv` -(if you are on one), run ``python ../../setup.py build --build-dir=.. install`` without this switch. +(if you are on one), run ``python ../../setup.py build --build-dir=.. --skip-build install`` without this switch. You should now have a working python binding (the ``dynet`` module). @@ -253,7 +253,7 @@ After running ``make -j 2``, you should have the files ``_dynet.so`` and ``_gdynet.so`` in the ``build/python`` folder. As before, ``cd build/python`` followed by -``python ../../setup.py build --build-dir=.. install --user`` will install the module. +``python ../../setup.py build --build-dir=.. --skip-build install --user`` will install the module. diff --git a/setup.py b/setup.py index 31a9ec4f2..1c3899b0c 100644 --- a/setup.py +++ b/setup.py @@ -158,10 +158,12 @@ def strip_lib(filename): class build(_build): user_options = [ ("build-dir=", None, "New or existing DyNet build directory."), + ("skip-build", None, "Assume DyNet C++ library is already built."), ] def __init__(self, *args, **kwargs): self.build_dir = None + self.skip_build = False _build.__init__(self, *args, **kwargs) def initialize_options(self): @@ -183,56 +185,56 @@ def run(self): log.info("EIGEN3_INCLUDE_DIR=" + EIGEN3_INCLUDE_DIR) log.info("CC_PATH=" + CC_PATH) log.info("CXX_PATH=" + CXX_PATH) - log.info("SCRIPT_DIR" + SCRIPT_DIR) - log.info("BUILD_DIR" + BUILD_DIR) + log.info("SCRIPT_DIR=" + SCRIPT_DIR) + log.info("BUILD_DIR=" + BUILD_DIR) log.info("INSTALL_PREFIX=" + INSTALL_PREFIX) log.info("PYTHON=" + PYTHON) run_process([CMAKE_PATH, "--version"]) run_process([CXX_PATH, "--version"]) - # Prepare folders - os.chdir(SCRIPT_DIR) - if not os.path.exists(BUILD_DIR): - log.info("Creating build directory " + BUILD_DIR) - os.makedirs(BUILD_DIR) - - os.chdir(BUILD_DIR) - if os.path.isdir(EIGEN3_INCLUDE_DIR): - log.info("Found eigen in " + EIGEN3_INCLUDE_DIR) - elif HG_PATH is None: - raise DistutilsSetupError("`hg` not found.") - else: - hg_cmd = [HG_PATH, "clone", "https://bitbucket.org/eigen/eigen"] - log.info("Cloning Eigen...") - if run_process(hg_cmd) != 0: - raise DistutilsSetupError(" ".join(hg_cmd)) - - os.environ["CXX"] = CXX_PATH - os.environ["CC"] = CC_PATH - - # Build module - cmake_cmd = [ - CMAKE_PATH, - SCRIPT_DIR, - "-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX, - "-DEIGEN3_INCLUDE_DIR=" + EIGEN3_INCLUDE_DIR, - "-DPYTHON=" + PYTHON, - ] - log.info("Configuring...") - if run_process(cmake_cmd) != 0: - raise DistutilsSetupError(" ".join(cmake_cmd)) - - make_cmd = [MAKE_PATH] + MAKE_FLAGS - log.info("Compiling...") - if run_process(make_cmd) != 0: - raise DistutilsSetupError(" ".join(make_cmd)) - BUILT_EXTENSIONS = True # because make calls build_ext - - make_cmd = [MAKE_PATH, "install"] - log.info("Installing...") - if run_process(make_cmd) != 0: - raise DistutilsSetupError(" ".join(make_cmd)) + if not self.skip_build: + # Prepare folders + if not os.path.isdir(BUILD_DIR): + log.info("Creating build directory " + BUILD_DIR) + os.makedirs(BUILD_DIR) + + os.chdir(BUILD_DIR) + if os.path.isdir(EIGEN3_INCLUDE_DIR): + log.info("Found eigen in " + EIGEN3_INCLUDE_DIR) + elif HG_PATH is None: + raise DistutilsSetupError("`hg` not found.") + else: + hg_cmd = [HG_PATH, "clone", "https://bitbucket.org/eigen/eigen"] + log.info("Cloning Eigen...") + if run_process(hg_cmd) != 0: + raise DistutilsSetupError(" ".join(hg_cmd)) + + os.environ["CXX"] = CXX_PATH + os.environ["CC"] = CC_PATH + + # Build module + cmake_cmd = [ + CMAKE_PATH, + SCRIPT_DIR, + "-DCMAKE_INSTALL_PREFIX=" + INSTALL_PREFIX, + "-DEIGEN3_INCLUDE_DIR=" + EIGEN3_INCLUDE_DIR, + "-DPYTHON=" + PYTHON, + ] + log.info("Configuring...") + if run_process(cmake_cmd) != 0: + raise DistutilsSetupError(" ".join(cmake_cmd)) + + make_cmd = [MAKE_PATH] + MAKE_FLAGS + log.info("Compiling...") + if run_process(make_cmd) != 0: + raise DistutilsSetupError(" ".join(make_cmd)) + + make_cmd = [MAKE_PATH, "install"] + log.info("Installing...") + if run_process(make_cmd) != 0: + raise DistutilsSetupError(" ".join(make_cmd)) + BUILT_EXTENSIONS = True # because make calls build_ext _build.run(self)