cezheng / Fuzi

A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
MIT License
1.06k stars 151 forks source link

Document encoding is not compiling on linux #115

Open gazsiazasz opened 3 years ago

gazsiazasz commented 3 years ago

On the system libxml2-dev package is installed, and /usr/include/libxml -> /usr/include/libxml2/libxml symlinked and /usr/include/libxml2/module.modulemap exists with the following content:

module libxml2 [system] [extern_c] {
    link "xml2"

    umbrella "libxml"
    export *
    module * { export * }
    exclude header "libxml/DOCBparser.h"
}

If I create a template swift package with Fuzi as a dependency in it:

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "spm-test",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "spm-test",
            targets: ["spm-test"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/cezheng/Fuzi.git", from: "3.1.2"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "spm-test",
            dependencies: ["Fuzi"]),
        .testTarget(
            name: "spm-testTests",
            dependencies: ["spm-test"]),
    ]
)

Compilation is failing on the following code snippet:

https://github.com/cezheng/Fuzi/blob/f08c8323da21e985f3772610753bcfc652c2103f/Sources/Document.swift#L34-L42

The error message I get is this:

/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:36:22: error: cannot find 'CFStringConvertIANACharSetNameToEncoding' in scope
      let encoding = CFStringConvertIANACharSetNameToEncoding(encodingName as CFString?)
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:37:22: error: cannot find 'kCFStringEncodingInvalidId' in scope
      if encoding != kCFStringEncodingInvalidId {
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:38:47: error: cannot find 'CFStringConvertEncodingToNSStringEncoding' in scope
        return String.Encoding(rawValue: UInt(CFStringConvertEncodingToNSStringEncoding(encoding)))
                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:36:22: error: cannot find 'CFStringConvertIANACharSetNameToEncoding' in scope
      let encoding = CFStringConvertIANACharSetNameToEncoding(encodingName as CFString?)
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:37:22: error: cannot find 'kCFStringEncodingInvalidId' in scope
      if encoding != kCFStringEncodingInvalidId {
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/spm-test/.build/checkouts/Fuzi/Sources/Document.swift:38:47: error: cannot find 'CFStringConvertEncodingToNSStringEncoding' in scope
        return String.Encoding(rawValue: UInt(CFStringConvertEncodingToNSStringEncoding(encoding)))

I saw that Kanna has something to patch this behavior but I am not sure how to implement it:

https://github.com/tid-kijyun/Kanna/blob/c657fb9f5827ef138068215c76ad0bb62bbc92da/Sources/Kanna/libxmlHTMLDocument.swift#L29

nighthawk commented 2 years ago

Do you by any chance have an example file which requires this code path? Then I can add this to my PR #117. Currently that PR just disables this code path on Linux.