Jelajahi Sumber

Local package step by step tutorial added (#208)

* Local package step by step tutorial

* seperate folder structure merged

* old page link removed

* Heading 2 -> 4
mccakit 8 bulan lalu
induk
melakukan
fe7094b2c8
1 mengubah file dengan 146 tambahan dan 1 penghapusan
  1. 146 1
      package/local_package.md

+ 146 - 1
package/local_package.md

@@ -1,4 +1,3 @@
-
 ### Default packaging format
 
 After version 2.5.5, we have provided a new local package packaging solution that will seamlessly integrate `add_requires` and `add_packages`.
@@ -158,6 +157,7 @@ $ xmake l find_package cmake::LibXml2
   }
 }
 ```
+
 #### Integrate the package in the project
 
 If we integrate and find cmake dependent packages in the xmake.lua project configuration, we usually don't need to use find_package directly, and we can use a more general and simple package integration method.
@@ -246,3 +246,148 @@ xmake will automatically append the following configuration internally when it l
 ```cmake
 find_package(ABC CONFIG REQUIRED)
 ```
+
+#### Step by Step Local Packaging Tutorial
+
+Written by [@mccakit](https://github.com/mccakit)
+
+---
+
+In this tutorial we will package a static library called foo, upload it to a GitHub repository and consume it similar to a manner of CMake FetchContent
+
+- Create an xmake project
+  
+  ```powershell
+  xmake create -P package_origin
+  ```
+
+- Imitate this filetree to prepare files for your package
+  
+  ```powershell
+  │   .gitignore
+  │   xmake.lua
+  │
+  └───src
+      │   main.cpp
+      │
+      ├───inc
+      │   └───foo
+      │           foo.hpp
+      │
+      └───lib
+          └───foo
+                  foo.cpp
+  ```
+
+- Create static library target in xmake
+  
+  ```lua
+  target("foo")
+      set_kind("static")
+      add_files("src/lib/foo/*.cpp")
+      add_headerfiles("src/inc/foo/*.hpp")
+      add_includedirs("src/inc/foo", {public = true})
+  ```
+
+- Implement the functionality of your target
+  
+  foo.hpp
+  
+  ```cpp
+  void foo();
+  ```
+  
+  foo.cpp
+  
+  ```cpp
+  #include <iostream>
+  #include "foo.hpp"
+  
+  void foo()
+  {
+      std::cout << "foo";
+  }
+  ```
+
+- Build your project and create the package
+  
+  ```powershell
+  xmake build
+  ```
+  
+  ```powershell
+  xmake package foo
+  ```
+
+- Delete the .gitignore file and create a github repository consisting of the build/package folder generated by xmake package command
+  
+  ```powershell
+  rm .gitignore
+  ```
+  
+  ```powershell
+  cp -r build/packages packages
+  ```
+  
+  ```powershell
+  git init
+  ```
+  
+  ```powershell
+  gh auth login
+  ```
+  
+  ```powershell
+  gh repo create xmake_local_package_tutorial --public --source=. --remote=origin --push
+  ```
+  
+  ```powershell
+  git add .\packages\
+  ```
+  
+  ```powershell
+  git commit -m "init"
+  ```
+  
+  ```powershell
+  git push
+  ```
+
+- Create a project where you intend on consuming the package
+  
+  ```powershell
+  xmake create -P package_consumption
+  ```
+
+- Consume the package by adding the repository, finding the package and then linking the package to target of your choosing
+  
+  ```lua
+  add_repositories("foo https://github.com/mccakit/xmake_local_package_tutorial.git")
+  add_requires("foo")
+  ```
+  
+  ```lua
+  target("package_consumption")
+      set_kind("binary")
+      add_files("src/*.cpp")
+      add_packages("foo")
+  ```
+  
+  ```cpp
+  #include "foo.hpp"
+  int main()
+  {
+      foo();
+      return 0;
+  }
+  ```
+
+---
+
+Congratulations, you have packaged a library and consumed it xmake!
+
+```powershell
+PS C:\Users\cakit\Desktop\package_consumption> xmake build -q
+PS C:\Users\cakit\Desktop\package_consumption> xmake run -q
+foo
+```