Thursday, November 25, 2021

[xgo] The first phase of hacking xgo

 xgo is a great Go CGO cross compiler that can help users to build the application written in Golang to multi-platforms at the same time. 

For more information in details, please check out these URLs:

https://github.com/karalabe/xgo
https://www.jianshu.com/p/a6047d3f976e

But, without any further study, we probably cannot understand how it works well. So here I just give some information about it. I will use my Github repository as the example:

# Install xgo
go get github.com/karalabe/xgo
$ git clone https://github.com/teyenliu/win-shared-example
$ cd win-shared-example

# Use xgo to cross-compile to windows/amd64
xgo -targets windows/amd64 github.com/teyenliu/win-shared-example


Then, we finish the cross-compilation for my application
And actually, xgo, this tool, is based on its prebuilt docker image: "karalabe/xgo-latest" to complete the cross-compilation task. We can check the dockerhub for it as well: https://hub.docker.com/r/karalabe/xgo-latest/builds

So, we can directly run this Docker Image to cross-compile my application based on my case as follows:

$ docker run --rm -v /home/liudanny/git/xgo:/build \
-v /home/liudanny/.xgo-cache:/deps-cache:ro \
-e REPO_REMOTE= \
-e REPO_BRANCH= \
-e PACK= \
-e DEPS= \
-e ARGS= \
-e OUT= \
-e FLAG_V=false \
-e FLAG_X=false \
-e FLAG_RACE=false \
-e FLAG_TAGS= \
-e FLAG_LDFLAGS= \
-e FLAG_BUILDMODE=default \
-e TARGETS=windows/amd64 \
-e EXT_GOPATH= \
karalabe/xgo-latest \
github.com/teyenliu/win-shared-example


Furthermore, we can directly run xgo's build.sh to cross-compile my application in the host instead of in in the container:

$ sudo su
# set Golang path
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

# rm /deps in advance
rm -rf /deps

# run build.sh
env GO111MODULE=off \
env REPO_REMOTE= \
env REPO_BRANCH= \
env PACK= \
env DEPS= \
env ARGS= \
env OUT= \
env FLAG_V=false \
env FLAG_X=false \
env FLAG_RACE=false \
env FLAG_TAGS= \
env FLAG_LDFLAGS= \
env FLAG_BUILDMODE=default \
env TARGETS=windows/amd64 \
env EXT_GOPATH= \
env BUILD_DEPS=/home/liudanny/git/xgo/docker/base/build_deps.sh \
env BOOTSTRAP_REPO=/home/liudanny/git/xgo/docker/base/bootstrap_repo.sh \
env BOOTSTRAP_PURE=/home/liudanny/git/xgo/docker/base/bootstrap_pure.sh \
env BOOTSTRAP=/home/liudanny/git/xgo/docker/base/bootstrap.sh \
env PATH=/usr/local/go/bin:$PATH \
./build.sh github.com/teyenliu/win-shared-example

Reference:
Golang 交叉编译
Golang交叉编译各个平台的二进制文件
Fyne Cross Compile


The following commands are not succesful to work for cross-compilation
$ CGO_ENABLED=1 CXX="x86_64-w64-mingw32-g++" CXX_FOR_TARGET="x86_64-w64-mingw32-g++" CC="x86_64-w64-mingw32-gcc" CC_FOR_TARGET="x86_64-w64-mingw32-gcc" GOOS=windows GOARCH=amd64 \
go build -ldflags -v -x -installsuffix cgo -o example.exe main.go

$ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -ldflags -v -x -installsuffix cgo -o example.exe main.go


Monday, November 22, 2021

[Fun] Detecting the license plate and replacing with a given image

Propably two years ago, I studied a ECCV 2018 paper "License Plate Detection and Recognition in Unconstrained Scenarios", and  its GitHub (https://github.com/sergiomsilva/alpr-unconstrained) repository contains the author's implementation. During that time, I had an idea to leveraged its implementation for replacing the license plate with my image. After tryed a couple days, I successed and provided a Python script to detect and replace cars' license plate in the video with a specified image.

I recently commited my modification code and pushed it to my forked repository as follows:
https://github.com/teyenliu/alpr-unconstrained

In this repository, it contains 2 samples which are convenient for users to test.

The way to process a video which will replace any car license plate in the video with a given image content.

$ python license-plate-replacement.py --video cars_mountain_short.mov --replace src/hello.PNG --output output1.avi
$ python license-plate-replacement.py --video cars_mountain.mp4 --replace src/hello.PNG --output output2.avi

The result looks like this:



Friday, November 19, 2021

[Golang] Golang with Cgo 動態連結(dynamic linking) 與 靜態連結(static linking)函式庫範例與筆記 in Windows environment

This post is focusd on how to do dynamic/static linking using Golang with cgo on Windows environment. If you want to see it on Linux environment, please check out another post: 

[Golang] Golang with Cgo 動態連結(dynamic linking) 函式庫範例與筆記 in Linux environment

First of all, thanks to someone who had posted several useful articles describing how to do dynamic linking and static linking using Golang(Go) with Cgo in Windows environment as follows:

P.S: For using this example on Linux environment, you should deal with the build process for the static and dynamic libraries.

在 Windows 環境建置動態連結函式庫 (Dynamic-link library),使用 MinGW gcc/g++ 以及 CodeBlock

Go with Cgo 靜態連結 (static linking) 函式庫建置範例與筆記
Go with Cgo 動態連結 (dynamic linking) 函式庫建置範例與筆記

Wednesday, November 3, 2021

[tmux] The most common used tmux commands

 This post can help you quickly learn how to use tmux in just a few minutes.

Get tmux session ready to start (new session)

# new session 
$ tmux new -s {session name}

# or new session without name
$ tmux

# list sessions
$ tmux ls

Split a tmux window into two panes

# <Ctrl+b> + %:split a tmux window into two panes vertically
# <Ctrl+b> + ":split a tmux window into two panes horizontally

# The way to switch to another pane
# <Ctrl + b> + <Arrow keys ← or → >

P.S: If you want to close one of the pane you created, 
you just switch on it and exit it by exit command.

For instance: split a tmux window into two panes vertically


Adjust a splitted pane size

You also are able to ajust  the splitted pane vertically or horizontally.

# The way to adjust a pane
# <Ctrl + b> + <Ctrl + Arrow keys  or  >

For instance: There are 5 panes in the window and I can adjust the currently focused pane.



Attach tmux session or detach it

# <Ctrl+b> + d:Put current session in the background running (detach)

# attach a background session
$ tmux attach-session -t {session name}

# Or
$ tmux a -t {session name}

Delete tmux session

$ tmux kill-session -t {session name}
$ tmux kill-session -t 0