1# Copyright (c) 2021 Huawei Device Co., Ltd.
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to
5# deal in the Software without restriction, including without limitation the
6# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7# sell copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19# IN THE SOFTWARE.
20
21import("//build/ohos.gni")
22
23LIBUSB_DIR = rebase_path("//third_party/libusb")
24
25action("libusb_action") {
26  script = "//third_party/libusb/install.sh"
27  outputs = [
28    "${target_gen_dir}/libusb-1.0.26/libusb/core.c",
29    "${target_gen_dir}/libusb-1.0.26/libusb/descriptor.c",
30    "${target_gen_dir}/libusb-1.0.26/libusb/hotplug.c",
31    "${target_gen_dir}/libusb-1.0.26/libusb/io.c",
32    "${target_gen_dir}/libusb-1.0.26/libusb/sync.c",
33    "${target_gen_dir}/libusb-1.0.26/libusb/strerror.c",
34  ]
35
36  if (is_mac) {
37    outputs += [
38      "${target_gen_dir}/libusb-1.0.26/libusb/os/darwin_usb.c",
39      "${target_gen_dir}/libusb-1.0.26/libusb/os/events_posix.c",
40      "${target_gen_dir}/libusb-1.0.26/libusb/os/threads_posix.c",
41    ]
42  } else if (is_mingw || is_win) {
43    outputs += [
44      "${target_gen_dir}/libusb-1.0.26/libusb/os/events_windows.c",
45      "${target_gen_dir}/libusb-1.0.26/libusb/os/threads_windows.c",
46      "${target_gen_dir}/libusb-1.0.26/libusb/os/windows_common.c",
47      "${target_gen_dir}/libusb-1.0.26/libusb/os/windows_usbdk.c",
48      "${target_gen_dir}/libusb-1.0.26/libusb/os/windows_winusb.c",
49    ]
50  } else if (is_linux || is_ohos) {
51    outputs += [
52      "${target_gen_dir}/libusb-1.0.26/libusb/os/events_posix.c",
53      "${target_gen_dir}/libusb-1.0.26/libusb/os/linux_netlink.c",
54      "${target_gen_dir}/libusb-1.0.26/libusb/os/linux_usbfs.c",
55      "${target_gen_dir}/libusb-1.0.26/libusb/os/threads_posix.c",
56    ]
57  }
58
59  inputs = [ "//third_party/libusb/libusb-1.0.26.tar.bz2" ]
60
61  libusb_path = rebase_path("${target_gen_dir}", root_build_dir)
62  libusb_source_path = rebase_path("//third_party/libusb", root_build_dir)
63  args = [
64    "$libusb_path",
65    "$libusb_source_path",
66  ]
67}
68
69# Configuration for libusb itself.
70config("libusb_config") {
71  include_dirs = [
72    get_label_info(":libusb_action", "target_gen_dir") +
73        "/libusb-1.0.26/libusb",
74    get_label_info(":libusb_action", "target_gen_dir") +
75        "/libusb-1.0.26/libusb/os",
76  ]
77  cflags = [
78    "-U__ANDROID__",
79    "-UUSE_UDEV",
80    "-Wno-#warnings",
81    "-Wno-error=sign-compare",
82    "-Wno-error=switch",
83    "-Wno-error=pragma-pack",
84  ]
85  if (is_linux || is_ohos) {
86    include_dirs += [ "${LIBUSB_DIR}/linux" ]
87    cflags += [ "-DPLATFORM_POSIX" ]
88  } else if (is_mingw || is_win) {
89    cflags += [
90      "-Werror",
91      "-Wno-unused-function",
92      "-Wno-unused-parameter",
93      "-DPLATFORM_WINDOWS",
94    ]
95    include_dirs += [ "${LIBUSB_DIR}/windows" ]
96  } else if (is_mac) {
97    cflags += [
98      "-Wno-unused-parameter",
99      "-Wno-deprecated-declarations",
100      "-DPLATFORM_POSIX",
101    ]
102    include_dirs += [ "${LIBUSB_DIR}/darwin" ]
103    frameworks = [
104      "CoreFoundation.framework",
105      "IOKit.framework",
106      "Security.framework",
107    ]
108    libs = [ "objc" ]
109  }
110}
111
112ohos_source_set("libusb_source") {
113  configs = [ ":libusb_config" ]
114  sources = get_target_outputs(":libusb_action")
115  deps = [ ":libusb_action" ]
116
117  subsystem_name = "thirdparty"
118  part_name = "libusb"
119}
120
121config("libusb_public_config") {
122  include_dirs = [
123    get_label_info(":libusb_action", "target_gen_dir") + "/libusb-1.0.26",
124    get_label_info(":libusb_action", "target_gen_dir") +
125        "/libusb-1.0.26/libusb",
126  ]
127}
128
129ohos_shared_library("libusb") {
130  deps = [ ":libusb_source" ]
131  public_configs = [ ":libusb_public_config" ]
132  output_name = "libusb_shared"
133  install_images = [ "system" ]
134  subsystem_name = "thirdparty"
135  part_name = "libusb"
136}
137