【 Homebrew series 】

  1. HomeBrew regular use tutorial
  2. Homebrew进阶使用教程(一)
  3. Homebrew Advanced Tutorial 2 – Build your own repository with a command line weather client
  4. Install and connect to Homebrew on MAC

I lot address: making address: https://github.com/rangaofei/homebrew-saka

The last article showed you how to create your own repository. This time, I’ll show you how to upload your own library or program to your repository. I am learning apUE, but I need to rely on an APUE library. This article will explain how to upload libraries to your own repository and then install them using BREW

1. Create an installation script

The first step is to compress your file into a tar.gz format. The name is written in the “library name-version number” format so that Homebrew can recognize its own version number automatically.

Here I have downloaded the source code of APue and compiled it. We only need two files: the apue.h file in the include folder and the libapue.a file in the lib folder. The command line goes into this folder and packages the two files:

The tar - CVZF apue 1.0. Tar. Gz. / *Copy the code

Automatically generates this file, upload the file to a everyone can access address, here I upload to https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz this address.

Execute the command

brew create <url>
Copy the code

To create our installation script, the script name defaults to the library name mentioned earlier, corresponding to my command

The brew the create https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gzCopy the code

The generated file is apue.rb.

2. Rewrite the installation script

Executing the above command will automatically open the installation script to the editable state. Here my computer automatically opens it with vim and generates a series of code:

# Documentation: https://docs.brew.sh/Formula-Cookbook.html
# http://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
 class Apue < Formula
  desc ""
  homepage ""
  url "https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz"
  sha256 "7e84d03563f7f0119f2d946cc9f439192e582b65a504c39f4158fea7f38f7cbd"
   def install
    # ENV.deparallelize
    system "./configure"."--disable-debug"."--disable-dependency-tracking"."--disable-silent-rules"."--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make"."install"
  end

   test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test apue`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end
Copy the code

Follow Ruby syntax (I don’t know Ruby at all, learn as you go).

Class is our target library. The important fields are desc,homepage, URL,sha256.

The URL and SHA256 are automatically generated during creation. The URL is the download address, and the SHA256 is the verification code. If the url does not match, the installation will stop. Homepage is the official website of the library.

3. Change the installation mode

The install function is an action that is performed at install time. By default, a make installation and a cmake (comment section) installation are provided. Instead of doing both this time, we will copy the file directly to the target folder.

def install
     lib.install "libapue.a"
     include.install "apue.h"
end
Copy the code

Note that the lib and include fields are basically the same as the install fields in cmake. Take a look at the official description:

prefix.install "file1"."file2" Install some files
prefix.install Dir["output/*"] Install all files in the entire Output folder
Copy the code

Prefix is a prefix that contains a list of destination folders for the package.

The prefix represents the folder

Prefix names Destination folder The sample
HOMEBREW_PREFIX /usr/local
prefix #{HOMEBREW_PREFIX}/Cellar/#{name}/#{version} / usr/local/Cellar/foo / 0.1
opt_prefix #{HOMEBREW_PREFIX}/opt/#{name} /usr/local/opt/foo
bin #{prefix}/bin / usr/local/Cellar/foo / 0.1 / bin
doc #{prefix}/share/doc/foo / usr/local/Cellar/foo / 0.1 / share/doc/foo
include #{prefix}/include / usr/local/Cellar/foo / 0.1 / include
info #{prefix}/share/info / usr/local/Cellar/foo / 0.1 / share/info
lib #{prefix}/lib / usr/local/Cellar/foo/lib / 0.1
libexec #{prefix}/libexec / usr/local / 0.1 / libexec Cellar/foo /
man #{prefix}/share/man / usr/local/Cellar/foo / 0.1 / share/man
man[1-8] #{prefix}/share/man/man[1-8] / usr/local/Cellar/foo / 0.1 / share/man/man [1-8]
sbin #{prefix}/sbin / usr/local/Cellar/foo / 0.1 / sbin
share #{prefix}/share / usr/local/Cellar/foo / 0.1 / share
pkgshare #{prefix}/share/foo / usr/local/Cellar/foo / 0.1 / share/foo
etc #{HOMEBREW_PREFIX}/etc /usr/local/etc
var #{HOMEBREW_PREFIX}/var /usr/local/var
buildpath A temporary directory somewhere on your system /private/tmp/[formula-name]-0q2b/[formula-name]

To explain:

Lib. Install libapue. A will libapue. A file is copied to/usr/local/Celler/apue/lib folder, Similarly include. Iinstall apue. H will apue. H file is copied to/usr/local/Celler apue/include directory.

4. Publish to the repository

Where is the ue. Rb file we just wrote? Brew creates this file in the core repository by default by executing the following command

cd $(brew --repo homebrew/core)
cd Formula/
ls |grep apue
Copy the code

You can see here that the output file contains the apue.rb we just edited. Copy it to your own repository folder and add git management to push it to the remote repository. At this point, others can just tap the repository and install the repository.

mv apue.rb .. /.. /.. /rangaofei/homebrew-saka/FormulaMove files to your own repository
cd $(brew --repo rangaofei/saka) Open your own warehouse
cd Formula  # Enter folder
git add --all Join git admin
git commit -m 'add new formula apue' # submit
git push # Remote repository submission
Copy the code

Then take a look at our library information

Brew Install apue to install the library. When the installation is successful, check to see if the corresponding files have been generated

You can see that two soft connections have been generated in the /usr/local/lib and usr/local/include folders to our brew installation directory. This is done automatically by Homebrew, and these soft connections are automatically removed when brew Uninstall APue is executed. So our library is released.

5. Verify usage

Now that we have the APue library installed, we are ready to use it immediately. Here I write my first example code in Clion:

#include "apue.h"
#include <dirent.h>

int main(int argc, char *argv[]) {
    DIR *dp;
    struct dirent *dirp;

    if(argc ! = 2) { err_quit("usage:ls directory_name");
    }

    if ((dp = opendir(argv[1])) == NULL) {
        err_sys("can't open %s", argv[1]);
    }
    while((dirp = readdir(dp)) ! = NULL) {printf("%s\n", dirp->d_name);
    }
    closedir(dp);
    exit(0);
}
Copy the code

The header file in the first line is the library we just installed, which can be referenced correctly here, and then modify the Cmakelists file

Cmake_minimum_required (VERSION 3.9) project (apue C)set(CMAKE_C_STANDARD 99)

add_executable(apue main.c)

target_link_libraries(apue apue.a)
Copy the code

In the last act we added the connection library apue.a to our library. Then perform an external build and enter the project directory

mkdir build
cd build/
cmake ..
make
Copy the code

No errors occurred during the build process and the execution file was printed correctly.

So far verify the thing