Build trial or commercial binaries and change runtime behavior accordingly

王林
Release: 2024-02-09 13:39:08
forward
909 people have browsed it

Build trial or commercial binaries and change runtime behavior accordingly

#php editor teaches you how to build trial or commercial binaries and change the runtime behavior accordingly. Whether to protect software copyright or to provide a trial version, building binaries is an important step. By changing the runtime behavior, some functional limitations or controls can be achieved. This article walks through the basic steps of building a binary and gives some examples of common runtime behavior modifications. Following this article, you will master the techniques of building binaries to add more value and protection to your software products.

Question content

Construction

I have this Makefile to build a binary for trial and commercial use:

BINARY_NAME=App.exe
 
trial:
    ENV_BUILD=trial go build -o ${BINARY_NAME}

comme:
    ENV_BUILD=comme go build -o ${BINARY_NAME}

clean:
    go clean
    rm ${BINARY_NAME}

prepare:
    go mod tidy
Copy after login

source code

In my source code I have an assertion Trial and Commercial restricted source code:

<code>package permit

import "fmt"

func Trial() (bool, error) {
    fmt.Println("You are using a limited trial release.")
    // ...
    // Assert limitations...
}

func Comme() (bool, error) {
    fmt.Println("You are using the unlimited commercial release.")
    // ...
}
</code>
Copy after login

I plan to call the above two functions at runtime as follows:

<code>package main

import "permit"

    // ...

    var builtTrial bool // TODO: Best way to detect if the built has been trial
    var builtComme bool // TODO: Best way to detect if the built has been commercial

    if builtTrial {
        permitted, err := permit.Trial()
    } else if builtComme {
        permitted, err := permit.Comme()
    }

    // ...
</code>
Copy after login

question

What is the best practice for detecting at runtime whether a binary is built as Trial or Commercial? I feel like I'm not aware of the standard tools available to do this.

Solution

Eventually, I continued to use Go to build the tag method:

File permit\build_Trial.go:

<code>//go:build trial

package permit

var buildType = "trial"
</code>
Copy after login

File permit\build_commercial.go:

<code>//go:build commercial

package permit

var buildType = "commercial"
</code>
Copy after login

Then, I can have statements like this:

<code>package permit

// ...

    if buildType == "trial" {
        fmt.Println("You are using a limited trial release.")
        return nil
    } else if buildType == "commercial" {
        fmt.Println("You are using the unlimited commercial release.")
        return nil
    } else {
        return fmt.Errorf("build type is neither trial nor commercial.")
    }
</code>
Copy after login

The final Makefile is:

BINARY_NAME=App.exe
 
trial:
    go build -tags trial -o ${BINARY_NAME}

commercial:
    go build -tags commercial -o ${BINARY_NAME}

clean:
    go clean
    rm ${BINARY_NAME}

prepare:
    go mod tidy
Copy after login

The above is the detailed content of Build trial or commercial binaries and change runtime behavior accordingly. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!