Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested cmd directories support #93

Open
alok87 opened this issue Oct 5, 2023 · 6 comments
Open

Nested cmd directories support #93

alok87 opened this issue Oct 5, 2023 · 6 comments
Assignees

Comments

@alok87
Copy link

alok87 commented Oct 5, 2023

cmd/acquiring/server
       /acquiring/migrate

Fails with:

$ BINS=acquiring/api make build

# building for darwin/arm64
binary: bin/darwin_arm64/acquiring/server  mv: rename .go/bin/darwin_arm64/acquiring/server to bin/darwin_arm64/acquiring/server: No such file or directory
make: *** [.go/bin/darwin_arm64/acquiring/server.stamp] Error 1
@alok87
Copy link
Author

alok87 commented Feb 23, 2024

@thockin what is required to be done to support nested folders

cmd/folder1/folder2/main.go
cmd/folder1/main.go

BINS=cmd/folder1 make build
BINS=cmd/folder1/folder make build

@alok87
Copy link
Author

alok87 commented Feb 23, 2024

# This is the target definition for all stampfiles.
# This will build the binary under ./.go and update the real binary iff needed.
STAMPS = $(foreach outbin,$(OUTBINS),.go/$(outbin).stamp)
.PHONY: $(STAMPS)
$(STAMPS): go-build
	echo -ne "binary: $(OUTBIN)  "
	if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then  \
	    mv .go/$(OUTBIN) $(OUTBIN);            \
	    date >$@;                              \
	    echo;                                  \
	else                                       \
	    echo "(cached)";                       \
	fi

This part is failing when BIN has a nested path BINS=acquiring/server

@alok87
Copy link
Author

alok87 commented Feb 23, 2024

Ok got it the issue is Makefile is expecting the binary to be created at bin/darwin_arm64/acquiring/server but build.sh does not generate like that!

@alok87
Copy link
Author

alok87 commented Feb 23, 2024

We need to use GOBIN in the build.sh as it is building in a flat out way and not following the same structure of cmd/acquring/server

@alok87
Copy link
Author

alok87 commented Feb 24, 2024

This can start throwing binaries at the required folder which Makefile expects, but need more polishing and also go install part in the end needs a fix.

./build.sh needs a fix with below:

# Iterate through all Go packages under github.com/XXX/XXX/cmd
for pkg in $(go list github.com/XXX/XXX/cmd/...); do
    relative_path=$(echo "${pkg}" | sed 's|^github.com/razorpay/upi-switch/||')
    if [ -f "${relative_path}/main.go" ]; then
        
        # Identify main packages and create binaries
        binary_name=$(basename "${relative_path}")
        binary_path=$(echo "${relative_path}" | sed 's|^cmd/||')

        echo "relative_path: ${relative_path}" 
        echo "binary_name: ${binary_name}"
        echo "binary_path: ${binary_path}"

        # Construct binary_install_path
        binary_install_path="${GOPATH}/bin/${binary_path}"

        # Ensure the directory structure exists
        mkdir -p "${binary_install_path}"

        echo "go install -o ${binary_install_path}/${binary_name} -ldflags '-X ${pkg}/pkg/version.Version=${VERSION}' ${relative_path}"
        echo "--"
    fi
done

@thockin
Copy link
Owner

thockin commented Feb 25, 2024

Note that GOBIN is hostile to cross-compiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants