diff --git a/README.md b/README.md index 25305fb..bfedbf4 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,9 @@ display.display(pd.DataFrame.from_records([["col 1": 3, "col 2": 5], ["col 1": 8 can import them: ```swift +// Specify SwiftPM flags to use during package installation. +%install-swiftpm-flags -c release + // Install the DeckOfPlayingCards package from GitHub. %install '.package(url: "https://github.com/NSHipster/DeckOfPlayingCards", from: "4.0.0")' DeckOfPlayingCards @@ -202,6 +205,8 @@ The next argument(s) to `%install` are the products that you want to install fro will refuse to install packages, and print out an error message explaining why, if you try to install packages in later cells.) * Downloads and build artifacts are not cached. +* `%install-swiftpm-flags` apply to all packages that you are installing; there + is no way to specify different flags for different packages. ## %include directives diff --git a/swift_kernel.py b/swift_kernel.py index 1e2a2ef..b9ad74e 100644 --- a/swift_kernel.py +++ b/swift_kernel.py @@ -382,13 +382,25 @@ def _process_installs(self, code): "%install" directives removed.""" processed_lines = [] all_packages = [] + all_swiftpm_flags = [] for index, line in enumerate(code.split('\n')): + line, swiftpm_flags = self._process_install_swiftpm_flags_line( + line) + all_swiftpm_flags += swiftpm_flags line, packages = self._process_install_line(index, line) processed_lines.append(line) all_packages += packages - self._install_packages(all_packages) + self._install_packages(all_packages, all_swiftpm_flags) return '\n'.join(processed_lines) + def _process_install_swiftpm_flags_line(self, line): + install_swiftpm_flags_match = re.match( + r'^\s*%install-swiftpm-flags (.*)$', line) + if install_swiftpm_flags_match is None: + return line, [] + flags = shlex.split(install_swiftpm_flags_match.group(1)) + return '', flags + def _process_install_line(self, line_index, line): install_match = re.match(r'^\s*%install (.*)$', line) if install_match is None: @@ -414,8 +426,8 @@ def _process_install_line(self, line_index, line): 'products': parsed[1:], }] - def _install_packages(self, packages): - if len(packages) == 0: + def _install_packages(self, packages, swiftpm_flags): + if len(packages) == 0 and len(swiftpm_flags) == 0: return # Appears to trigger even in the first cell execution. @@ -487,6 +499,10 @@ def _install_packages(self, packages): 'name': 'stdout', 'text': 'Installing packages:\n%s' % packages_human_description }) + self.send_response(self.iopub_socket, 'stream', { + 'name': 'stdout', + 'text': 'With SwiftPM flags: %s\n' % str(swiftpm_flags) + }) self.send_response(self.iopub_socket, 'stream', { 'name': 'stdout', 'text': 'Working in: %s\n' % scratchwork_base_path @@ -502,7 +518,8 @@ def _install_packages(self, packages): # == Ask SwiftPM to build the package == - build_p = subprocess.Popen([swift_build_path], stdout=subprocess.PIPE, + build_p = subprocess.Popen([swift_build_path] + swiftpm_flags, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=package_base_path) for build_output_line in iter(build_p.stdout.readline, b''): @@ -517,8 +534,9 @@ def _install_packages(self, packages): '%d.' % build_returncode) show_bin_path_result = subprocess.run( - [swift_build_path, '--show-bin-path'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE, cwd=package_base_path) + [swift_build_path, '--show-bin-path'] + swiftpm_flags, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + cwd=package_base_path) bin_dir = show_bin_path_result.stdout.decode('utf8').strip() lib_filename = os.path.join(bin_dir, 'libjupyterInstalledPackages.so') diff --git a/test/tests/notebooks/PackageWithC/Sources/PackageWithC2/sillyfunction2.c b/test/tests/notebooks/PackageWithC/Sources/PackageWithC2/sillyfunction2.c index 1966f8d..5674504 100644 --- a/test/tests/notebooks/PackageWithC/Sources/PackageWithC2/sillyfunction2.c +++ b/test/tests/notebooks/PackageWithC/Sources/PackageWithC2/sillyfunction2.c @@ -1,3 +1,3 @@ int sillyfunction2() { - return 1337; + return MACRO_DEFINED_IN_COMPILER_FLAG; } diff --git a/test/tests/notebooks/install_package_with_c.ipynb b/test/tests/notebooks/install_package_with_c.ipynb index 98904bb..7d81743 100644 --- a/test/tests/notebooks/install_package_with_c.ipynb +++ b/test/tests/notebooks/install_package_with_c.ipynb @@ -6,6 +6,7 @@ "metadata": {}, "outputs": [], "source": [ + "%install-swiftpm-flags -Xcc -DMACRO_DEFINED_IN_COMPILER_FLAG=1337\n", "%install '.package(path: \"$cwd/PackageWithC\")' PackageWithC" ] },