sgrif / mysqlclient-sys

Rust bindings for libmysqlclient
Apache License 2.0
37 stars 31 forks source link

Feature: statically link to mysqlclient #17

Closed Diggsey closed 5 months ago

Diggsey commented 5 years ago

This should work as closely as possible to the logic in pq-sys.

At a minimum, I would like an env var MYSQLCLIENT_LIB_STATIC which could be set to force static linking to mysql.

The workaround mentioned elsewhere (adding a build script with println!(r"cargo:rustc-link-lib=static=mysqlclient");) does not work because of the ordering of libraries in the link command. (liblibc ends up getting added before -lmysqlclient which causes unresolved symbol errors for all libc symbols used by mysqlclient but not used by rust)

In case anyone is interested, I was completely unable to compile mysqlclient statically for the musl target due to its ridiculous and broken build system, and the fact that it is written in C++. However I was able to build mariadbclient (written in C) for musl, and the client-side of mariadb is near enough identical that it should be usable in place of mysqlclient, at least if you stick to using mysqlclient's public, documented API.

Commands for obtaining mariadbclient in a system with musl-gcc:

            cd /tmp
            curl -LO https://downloads.mariadb.com/Connectors/c/connector-c-3.1.0/mariadb-connector-c-3.1.0-src.tar.gz
            tar xzf mariadb-connector-c-3.1.0-src.tar.gz
            mkdir build
            cd build
            sed 's/STRING(STRIP ${extra_dynamic_LDFLAGS} extra_dynamic_LDFLAGS)//' -i ../mariadb-connector-c-3.1.0-src/mariadb_config/CMakeLists.txt
            sed 's/LIST(REMOVE_DUPLICATES extra_dynamic_LDFLAGS)//' -i ../mariadb-connector-c-3.1.0-src/mariadb_config/CMakeLists.txt
            CC=musl-gcc LDFLAGS=-L/usr/local/musl/lib cmake -DOPENSSL_USE_STATIC_LIBS=1 -DWITH_SSL=/usr/local/musl -DWITH_CURL=0 ../mariadb-connector-c-3.1.0-src
            make mariadbclient
            sudo cp libmariadb/libmariadbclient.a /usr/local/musl/lib/libmysqlclient.a
            cd /home/rust/src
pzmarzly commented 5 years ago

If anyone is interested, I took the instruction above and made https://github.com/pzmarzly/mysqlclient-sys - fork containing a pre-built binary.

If your program uses Diesel and you want it to compile with musl, you can add this to your Cargo.toml:

[patch.crates-io]
mysqlclient-sys = { git = "https://github.com/pzmarzly/mysqlclient-sys", rev = "acd1b2b" }

Note the license change (explained in fork's README).

weiznich commented 5 months ago

Fixed with #39