From b4eeb5d25536ff1d4f56f62fb730042b7ccd6232 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 7 Jan 2021 11:44:31 +0100 Subject: [PATCH] Avoid compiling damlc twice This only compiles `damlc-bootstrap` as a Haskell binary and `damlc` is a simple symlink with additional runfiles. We now need to define `damlc@ghci` manually since it is no longer defined automatically. The manual definition was tested with ``` $ bazel run //:damlc@ghci --define ghci_data=True > :main ide -d ``` changelog_begin changelog_end --- bazel_tools/runfiles/defs.bzl | 42 +++++++++++++++++++++++++++ compiler/damlc/BUILD.bazel | 53 +++++++++++++++++------------------ 2 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 bazel_tools/runfiles/defs.bzl diff --git a/bazel_tools/runfiles/defs.bzl b/bazel_tools/runfiles/defs.bzl new file mode 100644 index 000000000000..0843f80c1699 --- /dev/null +++ b/bazel_tools/runfiles/defs.bzl @@ -0,0 +1,42 @@ +# Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +load("@os_info//:os_info.bzl", "is_windows") + +def _add_data_impl(ctx): + executable = ctx.actions.declare_file( + ctx.label.name + (".exe" if is_windows else ""), + ) + ctx.actions.symlink( + output = executable, + target_file = ctx.executable.executable, + is_executable = True, + ) + + runfiles = ctx.runfiles(files = [ctx.executable.executable] + ctx.files.data) + runfiles = runfiles.merge(ctx.attr.executable[DefaultInfo].default_runfiles) + for data_dep in ctx.attr.data: + runfiles = runfiles.merge(data_dep[DefaultInfo].default_runfiles) + + return [DefaultInfo( + executable = executable, + files = depset(direct = [executable]), + runfiles = runfiles, + )] + +add_data = rule( + _add_data_impl, + attrs = { + "executable": attr.label( + executable = True, + cfg = "target", + doc = "Create a symlink to this executable", + ), + "data": attr.label_list( + allow_files = True, + doc = "Add these data files to the executable's runfiles", + ), + }, + executable = True, + doc = "Creates a new target for the given executable with additional runfiles.", +) diff --git a/compiler/damlc/BUILD.bazel b/compiler/damlc/BUILD.bazel index 5ac35f9f9234..763ee934e600 100644 --- a/compiler/damlc/BUILD.bazel +++ b/compiler/damlc/BUILD.bazel @@ -1,42 +1,39 @@ # Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -load("//bazel_tools:haskell.bzl", "da_haskell_binary", "da_haskell_library", "da_haskell_test") +load("//bazel_tools:haskell.bzl", "da_haskell_binary", "da_haskell_library", "da_haskell_repl", "da_haskell_test") load("//rules_daml:daml.bzl", "daml_doc_test") load("@os_info//:os_info.bzl", "is_windows") load("//bazel_tools/packaging:packaging.bzl", "package_app") +load("//bazel_tools/runfiles:defs.bzl", "add_data") load(":util.bzl", "ghc_pkg") -da_haskell_binary( +damlc_data = [ + "//compiler/damlc/daml-ide-core:dlint.yaml", + "@static_asset_d3plus//:js/d3.min.js", + "@static_asset_d3plus//:js/d3plus.min.js", + ghc_pkg, + "//compiler/damlc:ghcversion", + "//compiler/damlc:hpp", + "//compiler/damlc/pkg-db", + "//compiler/damlc/stable-packages", + "//compiler/repl-service/server:repl_service_jar", + "//compiler/scenario-service/server:scenario_service_jar", +] + +add_data( name = "damlc", - srcs = ["exe/Main.hs"], + data = damlc_data, + executable = ":damlc-bootstrap", + visibility = ["//visibility:public"], +) - # We need to tell the linker to statically link pthread on Windows - # otherwise the library is not found at runtime. - compiler_flags = [ - "-optl-static", - "-optl-pthread", - ] if is_windows else [], - data = [ - "//compiler/damlc/daml-ide-core:dlint.yaml", - "@static_asset_d3plus//:js/d3.min.js", - "@static_asset_d3plus//:js/d3plus.min.js", - ghc_pkg, - "//compiler/damlc:ghcversion", - "//compiler/damlc:hpp", - "//compiler/damlc/pkg-db", - "//compiler/damlc/stable-packages", - "//compiler/repl-service/server:repl_service_jar", - "//compiler/scenario-service/server:scenario_service_jar", - ], - hackage_deps = [ - "base", - ], - src_strip_prefix = "exe", +da_haskell_repl( + name = "damlc@ghci", + data = damlc_data, + repl_ghci_commands = [":m Main"], visibility = ["//visibility:public"], - deps = [ - ":damlc-lib", - ], + deps = [":damlc-bootstrap"], ) genrule(