aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/ldflags.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-04-06 16:06:12 +0200
committerRon Evans <[email protected]>2021-04-09 18:33:48 +0200
commit33f76d1c2e914bd477bb19060670ae5c3608cf41 (patch)
tree50cd44f9bac6e426e9fd0e0098163616b531bc34 /testdata/ldflags.go
parentea8f7ba1f9b1e69a21c36ba3f7ed81097ea5e79f (diff)
downloadtinygo-33f76d1c2e914bd477bb19060670ae5c3608cf41.tar.gz
tinygo-33f76d1c2e914bd477bb19060670ae5c3608cf41.zip
main: implement -ldflags="-X ..."
This commit implements replacing some global variables with a different value, if the global variable has no initializer. For example, if you have: package main var version string you can replace the value with -ldflags="-X main.version=0.2". Right now it only works for uninitialized globals. The Go tooling also supports initialized globals (var version = "<undefined>") but that is a bit hard to combine with how initialized globals are currently implemented. The current implementation still allows caching package IR files while making sure the values don't end up in the build cache. This means compiling a program multiple times with different values will use the cached package each time, inserting the string value only late in the build process. Fixes #1045
Diffstat (limited to 'testdata/ldflags.go')
-rw-r--r--testdata/ldflags.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/testdata/ldflags.go b/testdata/ldflags.go
new file mode 100644
index 000000000..94db0dcb1
--- /dev/null
+++ b/testdata/ldflags.go
@@ -0,0 +1,9 @@
+package main
+
+// These globals can be changed using -ldflags="-X main.someGlobal=value".
+// At the moment, only globals without an initializer can be replaced this way.
+var someGlobal string
+
+func main() {
+ println("someGlobal:", someGlobal)
+}