From 2da00d8bdd15f79e6ac52d43873ea0007837b76f Mon Sep 17 00:00:00 2001 From: Johnson Shih Date: Thu, 13 Sep 2018 17:55:41 -0700 Subject: [PATCH 01/10] Create env.bat and setup.bat in build_cmake_package for Windows (#9) --- python/catkin/builder.py | 193 ++++++++++++++++++++++++--------------- 1 file changed, 119 insertions(+), 74 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 2b78a0714..f4c68e35c 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -469,6 +469,93 @@ def get_additional_environment(install, destdir, installspace): add_env['_CATKIN_SETUP_DIR'] = os.path.join(destdir, installspace[1:]) return add_env +def write_env_script_bat(dest_file, variables): + with open(dest_file, 'w') as f: + f.write("""\ +@echo off +REM generated from catkin.builder module + +if "%1"=="" ( + echo "Usage: env.bat COMMANDS" + echo "Calling env.bat without arguments is not supported anymore. Instead spawn a subshell and source a setup file manually." + exit 1 +) else ( + call "{SETUP_DIR}\\{SETUP_FILENAME}.bat" + %* +) +""".format(**variables)) + +def write_setup_script_bat(dest_file, last_setup_basename, variables): + with open(dest_file, 'w') as file_handle: + file_handle.write("""\ +@echo off +REM generated from catkin.builder module +""") + if last_setup_basename is not None: + file_handle.write('call "%s.bat"\n\n' % last_setup_basename) + file_handle.write("""\ +REM Prepend to the environment +set CMAKE_PREFIX_PATH={cmake_prefix_path};%CMAKE_PREFIX_PATH% +set LD_LIBRARY_PATH={ld_path};%LD_LIBRARY_PATH% +set PATH={path};%PATH% +set PKG_CONFIG_PATH={pkgcfg_path};%PKG_CONFIG_PATH% +set PYTHONPATH={pythonpath};%PYTHONPATH% +""".format(**variables)) + +def write_env_script_sh(dest_file, variables): + with open(os.path.join(dest_file), 'w') as f: + f.write("""\ +#!/usr/bin/env sh +# generated from catkin.builder module + +if [ $# -eq 0 ] ; then + /bin/echo "Usage: env.sh COMMANDS" + /bin/echo "Calling env.sh without arguments is not supported anymore. \ +Instead spawn a subshell and source a setup file manually." + exit 1 +fi + +# ensure to not use different shell type which was set before +CATKIN_SHELL=sh + +# source {SETUP_FILENAME}.sh from same directory as this file +. "$(cd "`dirname "$0"`" && pwd)/{SETUP_FILENAME}.sh" +exec "$@" +""".format(**variables)) + os.chmod(dest_file, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) + +def write_setup_script_sh(dest_file, last_setup_basename, variables): + with open(dest_file, 'w') as file_handle: + file_handle.write("""\ +#!/usr/bin/env sh +# generated from catkin.builder module + +# remember type of shell if not already set +if [ -z "$CATKIN_SHELL" ]; then + CATKIN_SHELL=sh +fi +""") + if last_setup_basename is not None: + file_handle.write('. "%s.$CATKIN_SHELL"\n\n' % last_setup_basename) + file_handle.write("""\ +# detect if running on Darwin platform +_UNAME=`uname -s` +IS_DARWIN=0 +if [ "$_UNAME" = "Darwin" ]; then + IS_DARWIN=1 +fi + +# Prepend to the environment +export CMAKE_PREFIX_PATH="{cmake_prefix_path}:$CMAKE_PREFIX_PATH" +if [ $IS_DARWIN -eq 0 ]; then + export LD_LIBRARY_PATH="{ld_path}:$LD_LIBRARY_PATH" +else + export DYLD_LIBRARY_PATH="{ld_path}:$DYLD_LIBRARY_PATH" +fi +export PATH="{path}:$PATH" +export PKG_CONFIG_PATH="{pkgcfg_path}:$PKG_CONFIG_PATH" +export PYTHONPATH="{pythonpath}:$PYTHONPATH" +""".format(**variables)) def build_cmake_package( path, package, @@ -553,14 +640,15 @@ def build_cmake_package( if last_env is not None: make_install_cmd = [last_env] + make_install_cmd run_command(make_install_cmd, build_dir, quiet) - - # If we are installing, and a env.sh exists, don't overwrite it - if install and os.path.exists(prefix_destdir(os.path.join(install_target, 'env.sh'), destdir)): + + env_script = 'env.bat' if sys.platform == 'win32' else 'env.sh' + # If we are installing, and a env script exists, don't overwrite it + if install and os.path.exists(prefix_destdir(os.path.join(install_target, env_script), destdir)): return - cprint(blue_arrow + " Generating an env.sh") - # Generate env.sh for chaining to catkin packages + cprint(blue_arrow + " Generating an " + env_script) + # Generate env script for chaining to catkin packages # except if using --merge which implies that new_env_path equals last_env - new_env_path = os.path.join(install_target, 'env.sh') + new_env_path = os.path.join(install_target, env_script) if install: new_env_path = prefix_destdir(new_env_path, destdir) if new_env_path != last_env: @@ -570,86 +658,43 @@ def build_cmake_package( } if not os.path.exists(os.path.dirname(new_env_path)): os.makedirs(os.path.dirname(new_env_path)) - with open(os.path.join(new_env_path), 'w') as f: - f.write("""\ -#!/usr/bin/env sh -# generated from catkin.builder module - -if [ $# -eq 0 ] ; then - /bin/echo "Usage: env.sh COMMANDS" - /bin/echo "Calling env.sh without arguments is not supported anymore. \ -Instead spawn a subshell and source a setup file manually." - exit 1 -fi - -# ensure to not use different shell type which was set before -CATKIN_SHELL=sh + env_script_writer = write_env_script_bat if sys.platform == 'win32' else write_env_script_sh + env_script_writer(dest_file = new_env_path, variables = variables) -# source {SETUP_FILENAME}.sh from same directory as this file -. "$(cd "`dirname "$0"`" && pwd)/{SETUP_FILENAME}.sh" -exec "$@" -""".format(**variables)) - os.chmod(new_env_path, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) - - # Generate setup.sh for chaining to catkin packages + # Generate setup script for chaining to catkin packages # except if using --merge which implies that new_setup_path equals last_setup_env - new_setup_path = os.path.join(install_target, 'setup.sh') + setup_script = 'setup.bat' if sys.platform == 'win32' else 'setup.sh' + new_setup_path = os.path.join(install_target, setup_script) if install: new_setup_path = prefix_destdir(new_setup_path, destdir) - last_setup_env = os.path.join(os.path.dirname(last_env), 'setup.sh') if last_env is not None else None + last_setup_env = os.path.join(os.path.dirname(last_env), setup_script) if last_env is not None else None if new_setup_path != last_setup_env: subs = {} - subs['cmake_prefix_path'] = install_target + ":" - subs['ld_path'] = os.path.join(install_target, 'lib') + ":" + subs['cmake_prefix_path'] = install_target + subs['ld_path'] = os.path.join(install_target, 'lib') pythonpath = os.path.join(install_target, get_python_install_dir()) - subs['pythonpath'] = pythonpath + ':' - subs['pkgcfg_path'] = os.path.join(install_target, 'lib', 'pkgconfig') + ":" - subs['path'] = os.path.join(install_target, 'bin') + ":" + subs['pythonpath'] = pythonpath + subs['pkgcfg_path'] = os.path.join(install_target, 'lib', 'pkgconfig') + subs['path'] = os.path.join(install_target, 'bin') arch = get_multiarch() if arch: - subs['ld_path'] += os.path.join(install_target, 'lib', arch) + ":" - subs['pkgcfg_path'] += os.path.join(install_target, 'lib', arch, 'pkgconfig') + ":" + subs['ld_path'] += os.path.join(install_target, 'lib', arch) + subs['pkgcfg_path'] += os.path.join(install_target, 'lib', arch, 'pkgconfig') if not os.path.exists(os.path.dirname(new_setup_path)): os.mkdir(os.path.dirname(new_setup_path)) - with open(new_setup_path, 'w') as file_handle: - file_handle.write("""\ -#!/usr/bin/env sh -# generated from catkin.builder module -# remember type of shell if not already set -if [ -z "$CATKIN_SHELL" ]; then - CATKIN_SHELL=sh -fi -""") - if last_env is not None: - file_handle.write('. "%s.$CATKIN_SHELL"\n\n' % last_setup_env[:-3]) - file_handle.write("""\ -# detect if running on Darwin platform -_UNAME=`uname -s` -IS_DARWIN=0 -if [ "$_UNAME" = "Darwin" ]; then - IS_DARWIN=1 -fi - -# Prepend to the environment -export CMAKE_PREFIX_PATH="{cmake_prefix_path}$CMAKE_PREFIX_PATH" -if [ $IS_DARWIN -eq 0 ]; then - export LD_LIBRARY_PATH="{ld_path}$LD_LIBRARY_PATH" -else - export DYLD_LIBRARY_PATH="{ld_path}$DYLD_LIBRARY_PATH" -fi -export PATH="{path}$PATH" -export PKG_CONFIG_PATH="{pkgcfg_path}$PKG_CONFIG_PATH" -export PYTHONPATH="{pythonpath}$PYTHONPATH" -""".format(**subs)) - - # generate setup.bash|zsh scripts - for shell in ['bash', 'zsh']: - setup_path = os.path.join(install_target, 'setup.%s' % shell) - if install: - setup_path = prefix_destdir(setup_path, destdir) - with open(setup_path, 'w') as f: - f.write("""\ + setup_script_writer = write_setup_script_bat if sys.platform == 'win32' else write_setup_script_sh + last_setup_basename = os.path.splitext(last_setup_env)[0] if last_setup_env is not None else None + setup_script_writer(dest_file = new_setup_path, last_setup_basename = last_setup_basename, variables = subs) + + if sys.platform != 'win32': + # generate setup.bash|zsh scripts + for shell in ['bash', 'zsh']: + setup_path = os.path.join(install_target, 'setup.%s' % shell) + if install: + setup_path = prefix_destdir(setup_path, destdir) + with open(setup_path, 'w') as f: + f.write("""\ #!/usr/bin/env {1} # generated from catkin.builder module From 9c94e467364c61f69e2c38df9710ac3bef0ea61c Mon Sep 17 00:00:00 2001 From: Johnson Shih Date: Fri, 14 Sep 2018 09:43:55 -0700 Subject: [PATCH 02/10] Skip the drive letter if run on Windows (#10) * Skip the drive letter if run on Windows * Remove unnecessary check for win32 --- python/catkin/builder.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index f4c68e35c..e08797f51 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -466,6 +466,8 @@ def has_make_target(path, target, use_ninja=False, use_nmake=False): def get_additional_environment(install, destdir, installspace): add_env = {} if install and destdir: + # skip the drive letter if on Windows + installspace = os.path.splitdrive(installspace)[1] add_env['_CATKIN_SETUP_DIR'] = os.path.join(destdir, installspace[1:]) return add_env @@ -764,6 +766,8 @@ def get_new_env(package, develspace, installspace, install, last_env, destdir=No def prefix_destdir(path, destdir=None): if destdir is not None: + # skip the drive letter if on Windows + path = os.path.splitdrive(path)[1] path = os.path.join(destdir, path[1:]) return path From d79f793f58ea5ab63f96c786de1068e85d34baa4 Mon Sep 17 00:00:00 2001 From: Johnson Shih Date: Fri, 14 Sep 2018 14:55:53 -0700 Subject: [PATCH 03/10] Provide top level devel space environment and setup bat files for Windows (#11) --- python/catkin/builder.py | 61 ++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index e08797f51..4b58fc278 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -1060,34 +1060,53 @@ def build_workspace_isolated( if not os.path.exists(develspace): os.makedirs(develspace) if not build_packages: - generated_env_sh = os.path.join(develspace, 'env.sh') - generated_setup_util_py = os.path.join(develspace, '_setup_util.py') - if not merge and pkg_develspace: - # generate env.sh and setup.sh|bash|zsh which relay to last devel space - with open(generated_env_sh, 'w') as f: - f.write("""\ + if sys.platform == 'win32': + env_script = 'env.bat' + env_script_content = """\ +@echo off +REM generated from catkin.builder module + +call {0} %* +""" + setup_script_content = """\ +@echo off +REM generated from catkin.builder module + +call "{0}/setup.{1}" +""" + else: + env_script = 'evn.sh' + env_script_content = """\ #!/usr/bin/env sh # generated from catkin.builder module {0} "$@" -""".format(os.path.join(pkg_develspace, 'env.sh'))) - os.chmod(generated_env_sh, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) - - for shell in ['sh', 'bash', 'zsh']: - with open(os.path.join(develspace, 'setup.%s' % shell), 'w') as f: - f.write("""\ +""" + setup_script_content = """\ #!/usr/bin/env {1} # generated from catkin.builder module . "{0}/setup.{1}" -""".format(pkg_develspace, shell)) +""" + generated_env_sh = os.path.join(develspace, env_script) + generated_setup_util_py = os.path.join(develspace, '_setup_util.py') + if not merge and pkg_develspace: + # generate env script and setup.sh|bash|zsh or setup.bat which relay to last devel space + with open(generated_env_sh, 'w') as f: + f.write(env_script_content.format(os.path.join(pkg_develspace, env_script))) + os.chmod(generated_env_sh, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) + + shells_to_write = ['bat'] if sys.platform == 'win32' else ['sh', 'bash', 'zsh'] + for shell in shells_to_write: + with open(os.path.join(develspace, 'setup.%s' % shell), 'w') as f: + f.write(setup_script_content.format(pkg_develspace, shell)) # remove _setup_util.py file which might have been generated for an empty devel space before if os.path.exists(generated_setup_util_py): os.remove(generated_setup_util_py) elif not pkg_develspace: - # generate env.sh and setup.sh|bash|zsh for an empty devel space + # generate env script and setup.sh|bash|zsh or setup.bat for an empty devel space if 'CMAKE_PREFIX_PATH' in os.environ.keys(): variables = { 'CATKIN_GLOBAL_BIN_DESTINATION': 'bin', @@ -1105,13 +1124,19 @@ def build_workspace_isolated( else: sys.exit("Unable to process CMAKE_PREFIX_PATH from environment. Cannot generate environment files.") - variables = {'SETUP_FILENAME': 'setup'} + variables = { + 'SETUP_DIR': develspace, + 'SETUP_FILENAME': 'setup' + } with open(generated_env_sh, 'w') as f: - f.write(configure_file(os.path.join(get_cmake_path(), 'templates', 'env.sh.in'), variables)) + f.write(configure_file(os.path.join(get_cmake_path(), 'templates', env_script + '.in'), variables)) os.chmod(generated_env_sh, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) - variables = {'SETUP_DIR': develspace} - for shell in ['sh', 'bash', 'zsh']: + variables = { + 'PYTHON_EXECUTABLE': sys.executable, + 'SETUP_DIR': develspace} + shells_to_write = ['bat'] if sys.platform == 'win32' else ['sh', 'bash', 'zsh'] + for shell in shells_to_write: with open(os.path.join(develspace, 'setup.%s' % shell), 'w') as f: f.write(configure_file( os.path.join(get_cmake_path(), 'templates', 'setup.%s.in' % shell), From 5c13cdc35af940a8d9e196d68d554b5bb3d3989b Mon Sep 17 00:00:00 2001 From: kejxu Date: Tue, 6 Nov 2018 18:26:17 -0800 Subject: [PATCH 04/10] use os.pathsep to join path components --- python/catkin/builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 4b58fc278..7c71c6156 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -503,7 +503,7 @@ def write_setup_script_bat(dest_file, last_setup_basename, variables): set PKG_CONFIG_PATH={pkgcfg_path};%PKG_CONFIG_PATH% set PYTHONPATH={pythonpath};%PYTHONPATH% """.format(**variables)) - + def write_env_script_sh(dest_file, variables): with open(os.path.join(dest_file), 'w') as f: f.write("""\ @@ -680,8 +680,8 @@ def build_cmake_package( subs['path'] = os.path.join(install_target, 'bin') arch = get_multiarch() if arch: - subs['ld_path'] += os.path.join(install_target, 'lib', arch) - subs['pkgcfg_path'] += os.path.join(install_target, 'lib', arch, 'pkgconfig') + subs['ld_path'] = os.pathsep.join([subs['ld_path'], os.path.join(install_target, 'lib', arch)]) + subs['pkgcfg_path'] = os.pathsep.join([subs['pkgcfg_path'], os.path.join(install_target, 'lib', arch, 'pkgconfig')]) if not os.path.exists(os.path.dirname(new_setup_path)): os.mkdir(os.path.dirname(new_setup_path)) From 27d3ae84e79a5105a70ae9761eeaea95a3821c3b Mon Sep 17 00:00:00 2001 From: kejxu Date: Tue, 6 Nov 2018 18:34:37 -0800 Subject: [PATCH 05/10] revert whitespace change --- python/catkin/builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 7c71c6156..d58bea478 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -503,7 +503,7 @@ def write_setup_script_bat(dest_file, last_setup_basename, variables): set PKG_CONFIG_PATH={pkgcfg_path};%PKG_CONFIG_PATH% set PYTHONPATH={pythonpath};%PYTHONPATH% """.format(**variables)) - + def write_env_script_sh(dest_file, variables): with open(os.path.join(dest_file), 'w') as f: f.write("""\ From 3aa35a6ea144aa1b88f548809aee32675163e2db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Or=C5=A1uli=C4=87?= Date: Tue, 27 Nov 2018 00:52:42 +0100 Subject: [PATCH 06/10] Fix path backslashes in catkin_package.cmake (#25) * Update catkin_package.cmake * Update catkin_package.cmake * Update builder.py * Normalize the path from subs['cmake_prefix_path'] * Revert the unrelated changes from my bad merge in the previous commit. Revert the unrelated changes from my bad merge in the previous commit to keep this PR clean. --- python/catkin/builder.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index d58bea478..b46b13ead 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -672,7 +672,8 @@ def build_cmake_package( last_setup_env = os.path.join(os.path.dirname(last_env), setup_script) if last_env is not None else None if new_setup_path != last_setup_env: subs = {} - subs['cmake_prefix_path'] = install_target + # CMAKE_PREFIX_PATH uses forward slash on all platforms. + subs['cmake_prefix_path'] = install_target.replace(os.sep, '/') subs['ld_path'] = os.path.join(install_target, 'lib') pythonpath = os.path.join(install_target, get_python_install_dir()) subs['pythonpath'] = pythonpath From 46f54780b922f5000a833600b27fb2536ac6635b Mon Sep 17 00:00:00 2001 From: James Xu Date: Wed, 9 Jan 2019 02:17:32 -0800 Subject: [PATCH 07/10] use underscope to exclude drive letter, and clarify there's no effect on Linux (#31) --- python/catkin/builder.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index b46b13ead..91e539c8a 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -466,8 +466,9 @@ def has_make_target(path, target, use_ninja=False, use_nmake=False): def get_additional_environment(install, destdir, installspace): add_env = {} if install and destdir: - # skip the drive letter if on Windows - installspace = os.path.splitdrive(installspace)[1] + # exclude drive letter if on Windows, returns the same string on Linux since there is no drive specifications + _, installspace = os.path.splitdrive(installspace) + add_env['_CATKIN_SETUP_DIR'] = os.path.join(destdir, installspace[1:]) return add_env @@ -767,8 +768,9 @@ def get_new_env(package, develspace, installspace, install, last_env, destdir=No def prefix_destdir(path, destdir=None): if destdir is not None: - # skip the drive letter if on Windows - path = os.path.splitdrive(path)[1] + # exclude drive letter if on Windows, returns the same string on Linux since there is no drive specifications + _, path = os.path.splitdrive(path) + path = os.path.join(destdir, path[1:]) return path From a6dfef31fa971901700cc4b2ed194356b30889f9 Mon Sep 17 00:00:00 2001 From: James Xu Date: Wed, 9 Jan 2019 13:13:57 -0800 Subject: [PATCH 08/10] Define string templates in separate steps and update comment (#32) * Create env.bat and setup.bat in build_cmake_package for Windows (#9) * define string templates in a separate step * change script writer function names * fix indentation for script template --- python/catkin/builder.py | 91 ++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 91e539c8a..5bfe96614 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -472,9 +472,9 @@ def get_additional_environment(install, destdir, installspace): add_env['_CATKIN_SETUP_DIR'] = os.path.join(destdir, installspace[1:]) return add_env -def write_env_script_bat(dest_file, variables): - with open(dest_file, 'w') as f: - f.write("""\ + +def write_env_bat(dest_file, variables): + env_bat_template = """\ @echo off REM generated from catkin.builder module @@ -482,32 +482,37 @@ def write_env_script_bat(dest_file, variables): echo "Usage: env.bat COMMANDS" echo "Calling env.bat without arguments is not supported anymore. Instead spawn a subshell and source a setup file manually." exit 1 -) else ( +) else ( call "{SETUP_DIR}\\{SETUP_FILENAME}.bat" %* ) -""".format(**variables)) +""" + with open(dest_file, 'w') as f: + f.write(env_bat_template.format(**variables)) + -def write_setup_script_bat(dest_file, last_setup_basename, variables): - with open(dest_file, 'w') as file_handle: - file_handle.write("""\ +def write_setup_bat(dest_file, last_setup_basename, variables): + setup_bat_header = """\ @echo off REM generated from catkin.builder module -""") - if last_setup_basename is not None: - file_handle.write('call "%s.bat"\n\n' % last_setup_basename) - file_handle.write("""\ +""" + setup_bat_template = """\ REM Prepend to the environment set CMAKE_PREFIX_PATH={cmake_prefix_path};%CMAKE_PREFIX_PATH% set LD_LIBRARY_PATH={ld_path};%LD_LIBRARY_PATH% set PATH={path};%PATH% set PKG_CONFIG_PATH={pkgcfg_path};%PKG_CONFIG_PATH% set PYTHONPATH={pythonpath};%PYTHONPATH% -""".format(**variables)) - -def write_env_script_sh(dest_file, variables): - with open(os.path.join(dest_file), 'w') as f: - f.write("""\ +""" + with open(dest_file, 'w') as f: + f.write(setup_bat_header) + if last_setup_basename is not None: + f.write('call "%s.bat"\n\n' % last_setup_basename) + f.write(setup_bat_template.format(**variables)) + + +def write_env_sh(dest_file, variables): + env_sh_template = """\ #!/usr/bin/env sh # generated from catkin.builder module @@ -524,12 +529,14 @@ def write_env_script_sh(dest_file, variables): # source {SETUP_FILENAME}.sh from same directory as this file . "$(cd "`dirname "$0"`" && pwd)/{SETUP_FILENAME}.sh" exec "$@" -""".format(**variables)) +""" + with open(os.path.join(dest_file), 'w') as f: + f.write(env_sh_template.format(**variables)) os.chmod(dest_file, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR) -def write_setup_script_sh(dest_file, last_setup_basename, variables): - with open(dest_file, 'w') as file_handle: - file_handle.write("""\ + +def write_setup_sh(dest_file, last_setup_basename, variables): + setup_sh_header = """\ #!/usr/bin/env sh # generated from catkin.builder module @@ -537,10 +544,8 @@ def write_setup_script_sh(dest_file, last_setup_basename, variables): if [ -z "$CATKIN_SHELL" ]; then CATKIN_SHELL=sh fi -""") - if last_setup_basename is not None: - file_handle.write('. "%s.$CATKIN_SHELL"\n\n' % last_setup_basename) - file_handle.write("""\ +""" + setup_sh_template = """\ # detect if running on Darwin platform _UNAME=`uname -s` IS_DARWIN=0 @@ -558,7 +563,13 @@ def write_setup_script_sh(dest_file, last_setup_basename, variables): export PATH="{path}:$PATH" export PKG_CONFIG_PATH="{pkgcfg_path}:$PKG_CONFIG_PATH" export PYTHONPATH="{pythonpath}:$PYTHONPATH" -""".format(**variables)) +""" + with open(dest_file, 'w') as f: + f.write(setup_sh_header) + if last_setup_basename is not None: + f.write('. "%s.$CATKIN_SHELL"\n\n' % last_setup_basename) + f.write(setup_sh_template.format(**variables)) + def build_cmake_package( path, package, @@ -643,9 +654,9 @@ def build_cmake_package( if last_env is not None: make_install_cmd = [last_env] + make_install_cmd run_command(make_install_cmd, build_dir, quiet) - - env_script = 'env.bat' if sys.platform == 'win32' else 'env.sh' - # If we are installing, and a env script exists, don't overwrite it + + env_script = 'env' + '.bat' if sys.platform == 'win32' else '.sh' + # If an env script already exists, don't overwrite it if install and os.path.exists(prefix_destdir(os.path.join(install_target, env_script), destdir)): return cprint(blue_arrow + " Generating an " + env_script) @@ -661,12 +672,12 @@ def build_cmake_package( } if not os.path.exists(os.path.dirname(new_env_path)): os.makedirs(os.path.dirname(new_env_path)) - env_script_writer = write_env_script_bat if sys.platform == 'win32' else write_env_script_sh + env_script_writer = write_env_bat if sys.platform == 'win32' else write_env_sh env_script_writer(dest_file = new_env_path, variables = variables) # Generate setup script for chaining to catkin packages # except if using --merge which implies that new_setup_path equals last_setup_env - setup_script = 'setup.bat' if sys.platform == 'win32' else 'setup.sh' + setup_script = 'setup' + '.bat' if sys.platform == 'win32' else '.sh' new_setup_path = os.path.join(install_target, setup_script) if install: new_setup_path = prefix_destdir(new_setup_path, destdir) @@ -686,25 +697,25 @@ def build_cmake_package( subs['pkgcfg_path'] = os.pathsep.join([subs['pkgcfg_path'], os.path.join(install_target, 'lib', arch, 'pkgconfig')]) if not os.path.exists(os.path.dirname(new_setup_path)): os.mkdir(os.path.dirname(new_setup_path)) - - setup_script_writer = write_setup_script_bat if sys.platform == 'win32' else write_setup_script_sh + setup_script_writer = write_setup_bat if sys.platform == 'win32' else write_setup_sh last_setup_basename = os.path.splitext(last_setup_env)[0] if last_setup_env is not None else None setup_script_writer(dest_file = new_setup_path, last_setup_basename = last_setup_basename, variables = subs) if sys.platform != 'win32': # generate setup.bash|zsh scripts - for shell in ['bash', 'zsh']: - setup_path = os.path.join(install_target, 'setup.%s' % shell) - if install: - setup_path = prefix_destdir(setup_path, destdir) - with open(setup_path, 'w') as f: - f.write("""\ + setup_script_template = """\ #!/usr/bin/env {1} # generated from catkin.builder module CATKIN_SHELL={1} . "{0}/setup.sh" -""".format(os.path.dirname(setup_path), shell)) +""" + for shell in ['bash', 'zsh']: + setup_path = os.path.join(install_target, 'setup.%s' % shell) + if install: + setup_path = prefix_destdir(setup_path, destdir) + with open(setup_path, 'w') as f: + f.write(setup_script_template.format(os.path.dirname(setup_path), shell)) def build_package( From c7ab5bd038b948f637a3e81b13603895ac546582 Mon Sep 17 00:00:00 2001 From: James Xu Date: Wed, 9 Jan 2019 13:41:49 -0800 Subject: [PATCH 09/10] fix env script name typo (#33) --- python/catkin/builder.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 5bfe96614..94d99c5c6 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -1074,8 +1074,9 @@ def build_workspace_isolated( if not os.path.exists(develspace): os.makedirs(develspace) if not build_packages: + env_script = 'env' if sys.platform == 'win32': - env_script = 'env.bat' + env_script += '.bat' env_script_content = """\ @echo off REM generated from catkin.builder module @@ -1089,7 +1090,7 @@ def build_workspace_isolated( call "{0}/setup.{1}" """ else: - env_script = 'evn.sh' + env_script += '.sh' env_script_content = """\ #!/usr/bin/env sh # generated from catkin.builder module @@ -1102,6 +1103,7 @@ def build_workspace_isolated( . "{0}/setup.{1}" """ + generated_env_sh = os.path.join(develspace, env_script) generated_setup_util_py = os.path.join(develspace, '_setup_util.py') if not merge and pkg_develspace: @@ -1120,7 +1122,7 @@ def build_workspace_isolated( os.remove(generated_setup_util_py) elif not pkg_develspace: - # generate env script and setup.sh|bash|zsh or setup.bat for an empty devel space + # generate env.* and setup.* scripts for an empty devel space if 'CMAKE_PREFIX_PATH' in os.environ.keys(): variables = { 'CATKIN_GLOBAL_BIN_DESTINATION': 'bin', @@ -1148,7 +1150,8 @@ def build_workspace_isolated( variables = { 'PYTHON_EXECUTABLE': sys.executable, - 'SETUP_DIR': develspace} + 'SETUP_DIR': develspace + } shells_to_write = ['bat'] if sys.platform == 'win32' else ['sh', 'bash', 'zsh'] for shell in shells_to_write: with open(os.path.join(develspace, 'setup.%s' % shell), 'w') as f: From a776de2775ac351a51dc48dd8ea5a7cb26ff84e0 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Mon, 28 Jan 2019 11:38:34 -0800 Subject: [PATCH 10/10] minor nitpicks --- python/catkin/builder.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python/catkin/builder.py b/python/catkin/builder.py index 94d99c5c6..0d121801e 100644 --- a/python/catkin/builder.py +++ b/python/catkin/builder.py @@ -476,7 +476,7 @@ def get_additional_environment(install, destdir, installspace): def write_env_bat(dest_file, variables): env_bat_template = """\ @echo off -REM generated from catkin.builder module +REM generated from catkin.builder Python module if "%1"=="" ( echo "Usage: env.bat COMMANDS" @@ -494,7 +494,7 @@ def write_env_bat(dest_file, variables): def write_setup_bat(dest_file, last_setup_basename, variables): setup_bat_header = """\ @echo off -REM generated from catkin.builder module +REM generated from catkin.builder Python module """ setup_bat_template = """\ REM Prepend to the environment @@ -514,7 +514,7 @@ def write_setup_bat(dest_file, last_setup_basename, variables): def write_env_sh(dest_file, variables): env_sh_template = """\ #!/usr/bin/env sh -# generated from catkin.builder module +# generated from catkin.builder Python module if [ $# -eq 0 ] ; then /bin/echo "Usage: env.sh COMMANDS" @@ -538,7 +538,7 @@ def write_env_sh(dest_file, variables): def write_setup_sh(dest_file, last_setup_basename, variables): setup_sh_header = """\ #!/usr/bin/env sh -# generated from catkin.builder module +# generated from catkin.builder Python module # remember type of shell if not already set if [ -z "$CATKIN_SHELL" ]; then @@ -673,7 +673,7 @@ def build_cmake_package( if not os.path.exists(os.path.dirname(new_env_path)): os.makedirs(os.path.dirname(new_env_path)) env_script_writer = write_env_bat if sys.platform == 'win32' else write_env_sh - env_script_writer(dest_file = new_env_path, variables = variables) + env_script_writer(dest_file=new_env_path, variables=variables) # Generate setup script for chaining to catkin packages # except if using --merge which implies that new_setup_path equals last_setup_env @@ -699,13 +699,13 @@ def build_cmake_package( os.mkdir(os.path.dirname(new_setup_path)) setup_script_writer = write_setup_bat if sys.platform == 'win32' else write_setup_sh last_setup_basename = os.path.splitext(last_setup_env)[0] if last_setup_env is not None else None - setup_script_writer(dest_file = new_setup_path, last_setup_basename = last_setup_basename, variables = subs) + setup_script_writer(dest_file=new_setup_path, last_setup_basename=last_setup_basename, variables=subs) if sys.platform != 'win32': # generate setup.bash|zsh scripts setup_script_template = """\ #!/usr/bin/env {1} -# generated from catkin.builder module +# generated from catkin.builder Python module CATKIN_SHELL={1} . "{0}/setup.sh" @@ -1079,13 +1079,13 @@ def build_workspace_isolated( env_script += '.bat' env_script_content = """\ @echo off -REM generated from catkin.builder module +REM generated from catkin.builder Python module call {0} %* """ setup_script_content = """\ @echo off -REM generated from catkin.builder module +REM generated from catkin.builder Python module call "{0}/setup.{1}" """ @@ -1093,13 +1093,13 @@ def build_workspace_isolated( env_script += '.sh' env_script_content = """\ #!/usr/bin/env sh -# generated from catkin.builder module +# generated from catkin.builder Python module {0} "$@" """ setup_script_content = """\ #!/usr/bin/env {1} -# generated from catkin.builder module +# generated from catkin.builder Python module . "{0}/setup.{1}" """ @@ -1142,7 +1142,7 @@ def build_workspace_isolated( variables = { 'SETUP_DIR': develspace, - 'SETUP_FILENAME': 'setup' + 'SETUP_FILENAME': 'setup', } with open(generated_env_sh, 'w') as f: f.write(configure_file(os.path.join(get_cmake_path(), 'templates', env_script + '.in'), variables)) @@ -1150,7 +1150,7 @@ def build_workspace_isolated( variables = { 'PYTHON_EXECUTABLE': sys.executable, - 'SETUP_DIR': develspace + 'SETUP_DIR': develspace, } shells_to_write = ['bat'] if sys.platform == 'win32' else ['sh', 'bash', 'zsh'] for shell in shells_to_write: