Skip to content

Commit

Permalink
Binary release of PodToBUILD / Bazel run support (#60)
Browse files Browse the repository at this point in the history
This PR adds the ability to perform release builds of `PodToBUILD`
through githubs command line automation.

Usage:
A user can point rules_pods at a release, and utilize the
`repository_rule`.

http_archive(
    name = "rules_pods",
    urls = ["https://github.com/pinterest/PodToBUILD/releases/download/__RELEASE__/PodToBUILD.zip"],
)

Additionally:

Add the ability to `bazel run` for vendorizing pods.

It creates a bazel target for `update_pods`. If the user is running a
release package, then the `Makefile` noops the build, otherwise, source
builds run with Bazel as expected. For this method, the user can add
rules_pods to the workspace in either a release `or` a source package.
  • Loading branch information
jerrymarino authored Oct 31, 2018
1 parent f3d6feb commit 781cb9f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ swift_library(
copts = ["-swift-version", "4", "-static-stdlib"],
)

alias(name = "update_pods", actual = "//bin:update_pods")

6 changes: 6 additions & 0 deletions BazelExtensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ py_binary(
visibility = ["//visibility:public"]
)

sh_library(
name = "vendored_srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"]
)

9 changes: 7 additions & 2 deletions Examples/BasiciOS/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# TODO: point this at a release ( this doesn't actually work because of
# compiling `RepoTools` ATM
# This is a fake repository - `override-repository` handles this in practice
git_repository(
name = "rules_pods",
remote = "https://github.com/pinterest/PodToBUILD.git",
branch = "master",
)

# In real usage, point rules_pods at a release.
#http_archive(
# name = "rules_pods",
# urls = ["https://github.com/Pinterst/PodToBUILD/releases/__RELEASE__.zip"],
#)

git_repository(
name = "build_bazel_rules_apple",
remote = "https://github.com/bazelbuild/rules_apple.git",
Expand Down
43 changes: 41 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ ci: clean
$(MAKE) build-test

release:
@tools/bazelwrapper build :RepoTools :Compiler
@tools/bazelwrapper build \
-c opt \
--swiftcopt=-whole-module-optimization :RepoTools :Compiler
@ditto bazel-bin/RepoTools bin/RepoTools
@ditto bazel-bin/Compiler bin/Compiler


# https://github.com/swift-vim/SwiftPackageManager.vim
compile_commands.json:
swift package clean
Expand All @@ -92,3 +93,41 @@ compile_commands.json:
-Xswiftc -parseable-output | tee .build/commands_build.log
cat .build/commands_build.log | spm-vim compile_commands



TESTED_BAZEL_VERSION=0.18.0

# Make a binary archive of PodToBUILD with the official github cli `hub`
github_release:
@which hub || (echo "this command relies on github cli" && exit 1)
@git diff --quiet || echo "Dirty tree" && exit 1
@git checkout master
@git pull --rebase origin master
@echo "creating release: $(TESTED_BAZEL_VERSION)-($(shell git rev-parse --short HEAD)"
$(MAKE) archive
@hub release create -p -a PodToBUILD.zip \
-m "PodToBUILD $(TESTED_BAZEL_VERSION)-$(shell git rev-parse --sort HEAD)" \
$(TESTED_BAZEL_VERSION)-$(shell git rev-parse --short HEAD)

# Create an archive of `rules_pods`.
# There should be no behaviorial differences between this package and a source
# checkout, other than the not building.
archive:
$(eval BUILD_DIR=$(shell mktemp -d))
@echo "Archiving to $(BUILD_DIR).."
@ditto bin $(BUILD_DIR)/bin
@ditto BazelExtensions $(BUILD_DIR)/BazelExtensions
@echo "release:\n\t@echo 'skipping build..'" > $(BUILD_DIR)/Makefile
@touch $(BUILD_DIR)/WORKSPACE
@echo "alias(name = 'update_pods', actual = '//bin:update_pods')" \
> $(BUILD_DIR)/BUILD
@ditto LICENSE $(BUILD_DIR)/
@cd $(BUILD_DIR) && zip -r \
$(PWD)/PodToBUILD.zip \
bin/* \
BazelExtensions \
Makefile \
WORKSPACE \
BUILD \
LICENSE

12 changes: 9 additions & 3 deletions bin/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Export this file for the repository_rule case
exports_files(["RepoTools"])

# `update_pods` Vendorizes pods
sh_binary(
name = "RepoToolsStub",
srcs = ["RepoToolsStub"],
visibility = ['//visibility:public']
name = "update_pods",
srcs = ["update_pods.py"],
# Load all vendored srcs into the program
data = ["//BazelExtensions:vendored_srcs", "RepoTools"],
visibility = ["//visibility:public"]
)

36 changes: 26 additions & 10 deletions bin/update_pods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
# update_pods.py Installs pods specified in Pods.WORKSPACE to $SRC_ROOT/Vendor/

from subprocess import Popen, PIPE
import os
Expand All @@ -11,12 +12,13 @@
# All known pods, populated after execing `Pods.WORKSPACE`
POD_PATHS = []

# RepoTools binary is adjacent to update_pods.py
def _getRepoToolPath():
return os.path.dirname(os.path.realpath(__file__)) + "/RepoTools"

def _exec(repository_ctx, command, cwd = None):
if repository_ctx.GetTrace():
print("__Running CMD", " ".join(command))
print("running: " + " ".join(command))
if cwd:
origWD = os.getcwd()
os.chdir(os.path.join(os.path.abspath(sys.path[0]), cwd))
Expand All @@ -28,7 +30,6 @@ def _exec(repository_ctx, command, cwd = None):
sys.exit(1)

if repository_ctx.GetTrace():
print("__Result")
print(result)
if cwd:
os.chdir(origWD)
Expand Down Expand Up @@ -271,9 +272,8 @@ def _update_repo_impl(repository_ctx):
["/bin/bash", "-c", script],
repository_ctx.GetPodRootDir())

version_file = open(repository_ctx.GetPodRootDir() + "/.pod-version", "w")
version_file.write(GetVersion(repository_ctx))
version_file.close()
with open(repository_ctx.GetPodRootDir() + "/.pod-version", "w") as version_file:
version_file.write(GetVersion(repository_ctx))

def new_pod_repository(name,
url = None,
Expand Down Expand Up @@ -370,7 +370,7 @@ def new_pod_repository(name,
src_root = SRC_ROOT)
_update_repo_impl(repository_ctx)

def cleanup_pods():
def _cleanupPods():
"""Cleanup removed Pods from Vendor"""
pods_dir = SRC_ROOT + "/Vendor"
known_pods = set(POD_PATHS)
Expand All @@ -383,16 +383,31 @@ def cleanup_pods():
continue
shutil.rmtree(full_path)


# Build a release of `RepoTools` if needed. Under a release package,
# the makefile is a noop.
def _buildRepoToolsRelease():
"""We currently rely on the `RepoTools` binary."""

print("Building PodToBUILD dependencies...")
_exec(RepositoryContext(None, None, None, None, None, None, None, trace=True), [
"make",
"release"],
os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

# If using `bazel run` to Vendorize pods, copy the contents required into
# //Vendor/rules_pods. This is required to ensure consistency.
def _vendorizeBazelExtensionsIfNeeded():
current_dir = os.path.dirname(os.path.realpath(__file__))
rules_pods_root = os.path.dirname(current_dir)
install_root = os.path.dirname(rules_pods_root)
if os.path.basename(install_root) == "external":
bazel_extension_dir = os.path.join(rules_pods_root, "BazelExtensions")
pods_dir = SRC_ROOT + "/Vendor"
rules_pods_root = os.path.join(pods_dir, "rules_pods")
vendor_path = os.path.join(rules_pods_root, "BazelExtensions")
if os.path.isdir(rules_pods_root):
shutil.rmtree(rules_pods_root)
os.makedirs(rules_pods_root)
shutil.copytree(bazel_extension_dir, vendor_path)

def main():
parser = argparse.ArgumentParser()
parser.add_argument("--src_root",
Expand Down Expand Up @@ -426,6 +441,7 @@ def main():
d = dict(locals(), **globals())
exec(workspace_str, d, d)

cleanup_pods()
_cleanupPods()
_vendorizeBazelExtensionsIfNeeded()

main()

0 comments on commit 781cb9f

Please sign in to comment.