Skip to content

Commit

Permalink
Add integration test coverage for make flag passing
Browse files Browse the repository at this point in the history
This requires making the make_simple Makefile more realistic by

* using CC instead of setting it and
* using CXXFLAGS instead of CXX_FLAGS and not overwriting its contents.
  • Loading branch information
fmeum committed Oct 5, 2021
1 parent cc369b8 commit 6272209
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
10 changes: 6 additions & 4 deletions examples/make_simple/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ load("@rules_foreign_cc//foreign_cc:defs.bzl", "make")

make(
name = "make_lib",
build_data = ["//make_simple/code:clang_wrapper.sh"],
env = {
"CLANG_WRAPPER": "$(execpath //make_simple/code:clang_wrapper.sh)",
},
copts = [
"-DREQUIRED_DEFINE",
],
lib_source = "//make_simple/code:srcs",
out_static_libs = ["liba.a"],
)
Expand All @@ -16,6 +15,9 @@ cc_test(
srcs = [
"test_libb.cpp",
],
copts = [
"-std=c++11",
],
tags = ["windows"],
deps = [
":make_lib",
Expand Down
7 changes: 3 additions & 4 deletions examples/make_simple/code/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
BUILD_DIR=build-out

UNAME:=$(shell uname)
CXX_FLAGS :=

ifneq (,$(findstring NT, $(UNAME)))
# If Windows
CXX_FLAGS := /MD
CXXFLAGS := $(CXXFLAGS) /MD
else
CXX_FLAGS := -fPIC
CXXFLAGS := $(CXXFLAGS) -fPIC
endif

default all $(BUILD_DIR)/lib/liba.a: liba.cpp liba.h
rm -rf $(BUILD_DIR)/lib
mkdir -p $(BUILD_DIR)/lib
$(CLANG_WRAPPER) $(CXX_FLAGS) -o $(BUILD_DIR)/lib/liba.o -c liba.cpp
$(CXX) $(CXXFLAGS) -o $(BUILD_DIR)/lib/liba.o -c liba.cpp
ar rcs $(BUILD_DIR)/lib/liba.a $(BUILD_DIR)/lib/liba.o

install: $(BUILD_DIR)/lib/liba.a
Expand Down
8 changes: 0 additions & 8 deletions examples/make_simple/code/clang_wrapper.sh

This file was deleted.

16 changes: 15 additions & 1 deletion examples/make_simple/code/liba.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#include "liba.h"

#include <math.h>

#ifndef REQUIRED_DEFINE
// '-DREQUIRED_DEFINE' is set via the copts attribute of the make rule.
#error "REQUIRED_DEFINE is not defined"
#endif

std::string hello_liba(void) {
return "Hello from LIBA!";
}
}

double hello_math(double a) {
// On Unix, this call requires linking to libm.so. The Bazel toolchain adds
// the required `-lm` linkopt automatically and rules_foreign_cc forwards
// this option to make invocation via the CXXFLAGS variable.
return acos(a);
}
3 changes: 2 additions & 1 deletion examples/make_simple/code/liba.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include <string>

std::string hello_liba(void);
double hello_math(double);

#endif
#endif
4 changes: 4 additions & 0 deletions examples/make_simple/test_libb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ int main(int argc, char* argv[])
if (result != "Hello from LIBA!") {
throw std::runtime_error("Wrong result: " + result);
}
double math_result = hello_math(0.5);
if (math_result < 1.047 || math_result > 1.048) {
throw std::runtime_error("Wrong math_result: " + std::to_string(math_result));
}
std::cout << "Everything's fine!";
}

0 comments on commit 6272209

Please sign in to comment.