This is an archive of the discontinued LLVM Phabricator instance.

[gn build] Add build files for LLVM unittests with a custom main() function
ClosedPublic

Authored by thakis on Jan 2 2019, 1:18 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

thakis created this revision.Jan 2 2019, 1:18 PM
phosek added inline comments.Jan 4 2019, 9:02 AM
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
53 ↗(On Diff #179931)

I'm not a big fan of the name, have you considered naming it e.g. gtest?

thakis marked an inline comment as done.Jan 4 2019, 11:02 AM
thakis added inline comments.
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
53 ↗(On Diff #179931)

unittest_with_custom_gtest? Or just gtest?

The "normal" unittest target is called unittest, and this one is a variant on that, so unittest_foo with some value for foo seems like a good name to me. The defining characteristic of these tests is that they contain a custom main() function, hence the current name. I'm very open for different naming suggestions, but imho this template should be named similarly to the existing unittest template. (Also, unittest is used like 50 times, this one only 3 times, so making it look familiar and self-consistent seems more important to me than making it short.)

Also, D56324 is another, similar patch that adds yet another similar template.

phosek added inline comments.Jan 4 2019, 6:50 PM
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
53 ↗(On Diff #179931)

How about just adding an argument to unittest template, e.g. has_main? It seems like that's the only difference between the two templates so that also avoids the duplication.

thakis marked an inline comment as done.Jan 4 2019, 7:05 PM
thakis added inline comments.
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
53 ↗(On Diff #179931)

Can templates have default arguments?

thakis marked an inline comment as done.Jan 4 2019, 8:07 PM
thakis added inline comments.
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
53 ↗(On Diff #179931)

Err sorry, I see what you mean. That's a good suggestion, will do.

thakis updated this revision to Diff 180353.Jan 4 2019, 8:33 PM
thakis edited the summary of this revision. (Show Details)

way better

thakis marked an inline comment as done.Jan 4 2019, 8:34 PM
thakis added inline comments.
llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
49 ↗(On Diff #180353)

I tried doing if (defined(invoker.has_custom_main) && invoker.has_custom_main) but then GN claimed:

FAILED: build.ninja 
../../../gn/out/gn --root=../.. -q --dotfile=../../llvm/utils/gn/.gn gen .
ERROR at //llvm/unittests/CodeGen/GlobalISel/BUILD.gn:19:21: Assignment had no effect.
  has_custom_main = true
                    ^---
You set the variable "has_custom_main" here and it was unused before it went
out of scope.
phosek accepted this revision.Jan 5 2019, 6:46 PM

LGTM

llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
49 ↗(On Diff #180353)

The way this is typically done is:

has_custom_main = false # default value
forward_variables_from(invoker, "*") # this is going to override the default value if set

That way you don't need the extra condition on lines 46-48.

This revision is now accepted and ready to land.Jan 5 2019, 6:46 PM
thakis marked an inline comment as done.Jan 6 2019, 7:10 AM

Thanks!

llvm/utils/gn/secondary/llvm/utils/unittest/unittest.gni
49 ↗(On Diff #180353)

Ah, that explains the "assignment had no effect" error: It doesn't complain about the assignment to has_custom_main in the template use (even though that's where the diag points to), but that forward_variables_from forwards that into the local scope here where it isn't used. If I change the forward_variables_from(invoker, "*") to forward_variables_from(invoker, "*", [ "has_custom_main" ]) then what I wanted to do works too.

Here's how each approach looks:

@@ -13,7 +17,7 @@
 template("unittest") {
   executable(target_name) {
     # Foward everything (configs, sources, deps, ...).
-    forward_variables_from(invoker, "*")
+    forward_variables_from(invoker, "*", [ "has_custom_main" ])
     assert(!defined(invoker.output_dir), "cannot set unittest output_dir")
     assert(!defined(invoker.testonly), "cannot set unittest testonly")
 
@@ -37,7 +41,12 @@ template("unittest") {
     # If you change output_dir here, look through
     # `git grep target_out_dir '*/unittests/*'` and update those too.
     output_dir = target_out_dir
-    deps += [ "//llvm/utils/unittest/UnitTestMain" ]
+
+    if (defined(invoker.has_custom_main) && invoker.has_custom_main) {
+      deps += [ "//llvm/utils/unittest:gtest" ]
+    } else {
+      deps += [ "//llvm/utils/unittest/UnitTestMain" ]
+    }

vs

@@ -12,7 +16,9 @@
 
 template("unittest") {
   executable(target_name) {
-    # Foward everything (configs, sources, deps, ...).
+    has_custom_main = false  # Default value.
+
+    # Foward everything (has_custom_main if set; configs, sources, deps, ...).
     forward_variables_from(invoker, "*")
     assert(!defined(invoker.output_dir), "cannot set unittest output_dir")
     assert(!defined(invoker.testonly), "cannot set unittest testonly")
@@ -37,7 +43,12 @@ template("unittest") {
     # If you change output_dir here, look through
     # `git grep target_out_dir '*/unittests/*'` and update those too.
     output_dir = target_out_dir
-    deps += [ "//llvm/utils/unittest/UnitTestMain" ]
+
+    if (has_custom_main) {
+      deps += [ "//llvm/utils/unittest:gtest" ]
+    } else {
+      deps += [ "//llvm/utils/unittest/UnitTestMain" ]
+    }

The first approach requires knowing about forward_variables_from's 3rd arg, but is slightly shorter. I went with the version that you suggested.

This revision was automatically updated to reflect the committed changes.