Repository: goreleaser/goreleaser
Pull request #1243 (2019-11-15 13:15:54)

feat: Add support for c-shared and c-archive build modes in windows
This PR modifies the default extension of windows that are build using the flag `-buildmode=c-shared` and `-buildmode=c-archive` from `.exe` to `.dll` and `.lib`, respectively.

Even though go allows other non-`.exe` build modes (archive, shared, pie and plugin), these are not supported in windows.

More info about build modes: https://golang.org/cmd/go/#hdr-Build_modes

This commit is based on this issue:
#1221 

caarlos0: closes #1221
codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@16cb4d8`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1243/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master    #1243   +/-   ##
=========================================
  Coverage          ?   83.68%           
=========================================
  Files             ?       58           
  Lines             ?     3316           
  Branches          ?        0           
=========================================
  Hits              ?     2775           
  Misses            ?      460           
  Partials          ?       81
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/pipe/build/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1243/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9idWlsZC9idWlsZC5nbw==) | `97.77% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=footer). Last update [16cb4d8...f61ad6b](https://codecov.io/gh/goreleaser/goreleaser/pull/1243?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go
index 18f0686f..3f8163c2 100644
--- a/internal/pipe/build/build.go
+++ b/internal/pipe/build/build.go
@@ -104,7 +104,7 @@ func runHook(ctx *context.Context, env []string, hook string) error {
 }
 
 func doBuild(ctx *context.Context, build config.Build, target string) error {
-	var ext = extFor(target)
+	var ext = extFor(target, build.Flags)
 
 	binary, err := tmpl.New(ctx).Apply(build.Binary)
 	if err != nil {
@@ -127,8 +127,16 @@ func doBuild(ctx *context.Context, build config.Build, target string) error {
 	})
 }
 
-func extFor(target string) string {
+func extFor(target string, flags config.FlagArray) string {
 	if strings.Contains(target, "windows") {
+		for _, s := range flags {
+			if s == "-buildmode=c-shared" {
+				return ".dll"
+			}
+			if s == "-buildmode=c-archive" {
+				return ".lib"
+			}
+		}
 		return ".exe"
 	}
 	if target == "js_wasm" {


Test patch:
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index 061b736b..dd985bf7 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -336,18 +336,23 @@ func TestDefaultFillSingleBuild(t *testing.T) {
 }
 
 func TestExtWindows(t *testing.T) {
-	assert.Equal(t, ".exe", extFor("windows_amd64"))
-	assert.Equal(t, ".exe", extFor("windows_386"))
+	assert.Equal(t, ".exe", extFor("windows_amd64", config.FlagArray{}))
+	assert.Equal(t, ".exe", extFor("windows_386", config.FlagArray{}))
+	assert.Equal(t, ".exe", extFor("windows_amd64", config.FlagArray{"-tags=dev", "-v"}))
+	assert.Equal(t, ".dll", extFor("windows_amd64", config.FlagArray{"-tags=dev", "-v", "-buildmode=c-shared"}))
+	assert.Equal(t, ".dll", extFor("windows_386", config.FlagArray{"-buildmode=c-shared"}))
+	assert.Equal(t, ".lib", extFor("windows_amd64", config.FlagArray{"-buildmode=c-archive"}))
+	assert.Equal(t, ".lib", extFor("windows_386", config.FlagArray{"-tags=dev", "-v", "-buildmode=c-archive"}))
 }
 
 func TestExtWasm(t *testing.T) {
-	assert.Equal(t, ".wasm", extFor("js_wasm"))
+	assert.Equal(t, ".wasm", extFor("js_wasm", config.FlagArray{}))
 }
 
 func TestExtOthers(t *testing.T) {
-	assert.Empty(t, "", extFor("linux_amd64"))
-	assert.Empty(t, "", extFor("linuxwin_386"))
-	assert.Empty(t, "", extFor("winasdasd_sad"))
+	assert.Empty(t, "", extFor("linux_amd64", config.FlagArray{}))
+	assert.Empty(t, "", extFor("linuxwin_386", config.FlagArray{}))
+	assert.Empty(t, "", extFor("winasdasd_sad", config.FlagArray{}))
 }
 
 func TestTemplate(t *testing.T) {