forked from GNOME/libxml2
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-wasm.sh
More file actions
executable file
·862 lines (728 loc) · 24.9 KB
/
Copy pathbuild-wasm.sh
File metadata and controls
executable file
·862 lines (728 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/bin/bash
# libxml2.wasm Production Build Script
# High-performance XML processing with WebAssembly
set -e
# Build configuration
BUILD_DIR="build-wasm"
DIST_DIR="dist"
INSTALL_DIR="install"
# Build configuration for high-performance XML processing
INITIAL_MEMORY="64MB"
MAXIMUM_MEMORY="512MB"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check for required tools
check_dependencies() {
print_status "Checking build dependencies..."
if ! command -v emcc &> /dev/null; then
print_error "Emscripten not found. Please install and activate emsdk"
exit 1
fi
if ! command -v cmake &> /dev/null; then
print_error "CMake not found. Please install cmake"
exit 1
fi
# Check for zlib.wasm dependency
if [ ! -d "../zlib.wasm/dist" ]; then
print_warning "zlib.wasm not found. Building without zlib support"
USE_ZLIB=OFF
else
print_success "Found zlib.wasm dependency"
USE_ZLIB=ON
fi
# Check for icu.wasm dependency
if [ ! -d "../icu.wasm/dist" ]; then
print_warning "icu.wasm not found. Building without ICU support"
USE_ICU=OFF
else
print_success "Found icu.wasm dependency"
USE_ICU=ON
fi
print_success "All dependencies checked"
}
# Clean previous builds
clean_build() {
print_status "Cleaning previous builds..."
rm -rf ${BUILD_DIR} ${DIST_DIR} ${INSTALL_DIR}
mkdir -p ${BUILD_DIR} ${DIST_DIR} ${INSTALL_DIR}
print_success "Build directories cleaned"
}
# Configure CMake for WASM build
configure_cmake() {
print_status "Configuring CMake for WebAssembly..."
cd ${BUILD_DIR}
# WASM-specific CMake configuration for high performance
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=../${INSTALL_DIR} \
\
`# Build configuration` \
-DBUILD_SHARED_LIBS=OFF \
\
`# Core XML features for comprehensive processing` \
-DLIBXML2_WITH_TREE=ON \
-DLIBXML2_WITH_PARSER=ON \
-DLIBXML2_WITH_SAX1=ON \
-DLIBXML2_WITH_PUSH=ON \
-DLIBXML2_WITH_READER=ON \
-DLIBXML2_WITH_OUTPUT=ON \
-DLIBXML2_WITH_WRITER=ON \
-DLIBXML2_WITH_XPATH=ON \
-DLIBXML2_WITH_XINCLUDE=ON \
-DLIBXML2_WITH_VALID=ON \
-DLIBXML2_WITH_SCHEMAS=ON \
-DLIBXML2_WITH_RELAXNG=ON \
-DLIBXML2_WITH_PATTERN=ON \
-DLIBXML2_WITH_REGEXPS=ON \
-DLIBXML2_WITH_HTML=ON \
-DLIBXML2_WITH_CATALOG=ON \
\
`# Disable WASM-incompatible features` \
-DLIBXML2_WITH_THREADS=OFF \
-DLIBXML2_WITH_TLS=OFF \
-DLIBXML2_WITH_HTTP=OFF \
-DLIBXML2_WITH_MODULES=OFF \
-DLIBXML2_WITH_PROGRAMS=OFF \
-DLIBXML2_WITH_PYTHON=OFF \
-DLIBXML2_WITH_LEGACY=OFF \
-DLIBXML2_WITH_TESTS=OFF \
-DLIBXML2_WITH_DOCS=OFF \
-DLIBXML2_WITH_READLINE=OFF \
\
`# Encoding configuration` \
-DLIBXML2_WITH_ICONV=OFF \
-DLIBXML2_WITH_ISO8859X=ON \
-DLIBXML2_WITH_ICU=${USE_ICU} \
\
`# Compression support` \
-DLIBXML2_WITH_ZLIB=${USE_ZLIB} \
-DLIBXML2_WITH_LZMA=OFF \
\
`# Debug configuration` \
-DLIBXML2_WITH_DEBUG=ON
cd ..
print_success "CMake configuration completed"
}
# Build the library
build_library() {
print_status "Building libxml2.wasm..."
cd ${BUILD_DIR}
make -j$(nproc)
make install
cd ..
print_success "Library build completed"
}
# Create JavaScript API wrapper
create_js_wrapper() {
print_status "Creating JavaScript API wrapper..."
# Create the WASM wrapper for optimal performance
cat > ${DIST_DIR}/libxml2-wasm.js << 'EOF'
/**
* libxml2.wasm - Production XML Processing Library
* High-performance XML parsing, validation, and transformation
*
* Copyright 2025 Superstruct Ltd, New Zealand
* Licensed under MIT License
*/
class LibXML2WASM {
constructor(wasmModule) {
this.Module = wasmModule;
this.initialized = false;
this.documents = new Map();
this.nextDocId = 1;
// Performance tracking
this.stats = {
documentsProcessed: 0,
totalParseTime: 0,
validationTime: 0
};
// Initialize function bindings
this._initializeFunctions();
}
_initializeFunctions() {
// Core XML parsing functions
this._xmlParseMemory = this.Module.cwrap('xmlParseMemory', 'number', ['number', 'number']);
this._xmlParseFile = this.Module.cwrap('xmlParseFile', 'number', ['string']);
this._xmlFreeDoc = this.Module.cwrap('xmlFreeDoc', 'null', ['number']);
this._xmlDocGetRootElement = this.Module.cwrap('xmlDocGetRootElement', 'number', ['number']);
// Node operations
this._xmlGetProp = this.Module.cwrap('xmlGetProp', 'string', ['number', 'string']);
this._xmlSetProp = this.Module.cwrap('xmlSetProp', 'number', ['number', 'string', 'string']);
this._xmlNodeGetContent = this.Module.cwrap('xmlNodeGetContent', 'string', ['number']);
this._xmlNodeSetContent = this.Module.cwrap('xmlNodeSetContent', 'null', ['number', 'string']);
// XPath functions
this._xmlXPathNewContext = this.Module.cwrap('xmlXPathNewContext', 'number', ['number']);
this._xmlXPathFreeContext = this.Module.cwrap('xmlXPathFreeContext', 'null', ['number']);
this._xmlXPathEvalExpression = this.Module.cwrap('xmlXPathEvalExpression', 'number', ['string', 'number']);
this._xmlXPathFreeObject = this.Module.cwrap('xmlXPathFreeObject', 'null', ['number']);
// Validation functions
this._xmlSchemaNewDocParserCtxt = this.Module.cwrap('xmlSchemaNewDocParserCtxt', 'number', ['number']);
this._xmlSchemaParse = this.Module.cwrap('xmlSchemaParse', 'number', ['number']);
this._xmlSchemaNewValidCtxt = this.Module.cwrap('xmlSchemaNewValidCtxt', 'number', ['number']);
this._xmlSchemaValidateDoc = this.Module.cwrap('xmlSchemaValidateDoc', 'number', ['number', 'number']);
// Output functions
this._xmlSaveToBuffer = this.Module.cwrap('xmlSaveToBuffer', 'number', ['number', 'string', 'number']);
// Error handling
this._xmlGetLastError = this.Module.cwrap('xmlGetLastError', 'number', []);
// Memory management
this._xmlFree = this.Module.cwrap('xmlFree', 'null', ['number']);
this._xmlMalloc = this.Module.cwrap('xmlMalloc', 'number', ['number']);
}
/**
* Initialize the libxml2.wasm module
* @param {Object} options - Configuration options
* @returns {Promise<boolean>} Success status
*/
async initialize(options = {}) {
if (this.initialized) {
return true;
}
try {
// Initialize libxml2 parser
this.Module.ccall('xmlInitParser', null, []);
// Configure parser options
if (options.loadExtDtd !== false) {
this.Module.ccall('xmlLoadExtDtdDefaultValue', null, ['number'], [1]);
}
if (options.substituteEntities !== false) {
this.Module.ccall('xmlSubstituteEntitiesDefaultValue', null, ['number'], [1]);
}
this.initialized = true;
console.log('✅ libxml2.wasm initialized successfully');
return true;
} catch (error) {
console.error('❌ Failed to initialize libxml2.wasm:', error);
return false;
}
}
/**
* Parse XML from string
* @param {string} xmlString - XML content to parse
* @param {Object} options - Parser options
* @returns {Object} Parsed document object
*/
parseString(xmlString, options = {}) {
const startTime = performance.now();
try {
// Allocate memory for XML string
const xmlBuffer = this.Module._malloc(xmlString.length + 1);
this.Module.writeStringToMemory(xmlString, xmlBuffer);
// Parse the XML
const docPtr = this._xmlParseMemory(xmlBuffer, xmlString.length);
this.Module._free(xmlBuffer);
if (!docPtr) {
throw new Error('Failed to parse XML: Invalid XML syntax');
}
// Create document wrapper
const docId = this.nextDocId++;
const doc = new XMLDocument(this, docPtr, docId);
this.documents.set(docId, doc);
// Update statistics
const parseTime = performance.now() - startTime;
this.stats.documentsProcessed++;
this.stats.totalParseTime += parseTime;
return doc;
} catch (error) {
throw new Error(`XML parsing failed: ${error.message}`);
}
}
/**
* Parse XML from file (Node.js environments)
* @param {string} filename - Path to XML file
* @param {Object} options - Parser options
* @returns {Object} Parsed document object
*/
parseFile(filename, options = {}) {
const startTime = performance.now();
try {
const docPtr = this._xmlParseFile(filename);
if (!docPtr) {
throw new Error(`Failed to parse XML file: ${filename}`);
}
const docId = this.nextDocId++;
const doc = new XMLDocument(this, docPtr, docId);
this.documents.set(docId, doc);
const parseTime = performance.now() - startTime;
this.stats.documentsProcessed++;
this.stats.totalParseTime += parseTime;
return doc;
} catch (error) {
throw new Error(`XML file parsing failed: ${error.message}`);
}
}
/**
* Validate XML document against schema
* @param {XMLDocument} doc - Document to validate
* @param {XMLDocument} schema - Schema document
* @returns {Object} Validation result
*/
validateDocument(doc, schema) {
const startTime = performance.now();
try {
const parserCtxt = this._xmlSchemaNewDocParserCtxt(schema.docPtr);
if (!parserCtxt) {
throw new Error('Failed to create schema parser context');
}
const schemaPtr = this._xmlSchemaParse(parserCtxt);
if (!schemaPtr) {
throw new Error('Failed to parse schema');
}
const validCtxt = this._xmlSchemaNewValidCtxt(schemaPtr);
if (!validCtxt) {
throw new Error('Failed to create validation context');
}
const result = this._xmlSchemaValidateDoc(validCtxt, doc.docPtr);
const validationTime = performance.now() - startTime;
this.stats.validationTime += validationTime;
return {
valid: result === 0,
errors: result === 0 ? [] : this._getValidationErrors()
};
} catch (error) {
throw new Error(`Document validation failed: ${error.message}`);
}
}
/**
* Get performance statistics
* @returns {Object} Performance metrics
*/
getStats() {
return {
...this.stats,
avgParseTime: this.stats.totalParseTime / this.stats.documentsProcessed || 0,
avgValidationTime: this.stats.validationTime / this.stats.documentsProcessed || 0
};
}
/**
* Cleanup resources
*/
cleanup() {
// Free all documents
for (const doc of this.documents.values()) {
doc._cleanup();
}
this.documents.clear();
// Cleanup libxml2
this.Module.ccall('xmlCleanupParser', null, []);
this.initialized = false;
}
/**
* Get validation errors from libxml2
* @private
*/
_getValidationErrors() {
const errors = [];
const errorPtr = this._xmlGetLastError();
if (errorPtr) {
// Extract error information
// This would need proper struct parsing based on xmlError
errors.push({
message: "Validation error occurred",
line: 0,
column: 0
});
}
return errors;
}
}
/**
* XML Document wrapper class
*/
class XMLDocument {
constructor(libxml, docPtr, docId) {
this.libxml = libxml;
this.docPtr = docPtr;
this.docId = docId;
}
/**
* Get the root element
* @returns {XMLElement} Root element
*/
getRoot() {
const rootPtr = this.libxml._xmlDocGetRootElement(this.docPtr);
if (!rootPtr) {
throw new Error('Document has no root element');
}
return new XMLElement(this.libxml, rootPtr);
}
/**
* Evaluate XPath expression
* @param {string} xpath - XPath expression
* @returns {Array} Matching nodes
*/
xpath(xpath) {
const xpathCtxt = this.libxml._xmlXPathNewContext(this.docPtr);
if (!xpathCtxt) {
throw new Error('Failed to create XPath context');
}
try {
const resultPtr = this.libxml._xmlXPathEvalExpression(xpath, xpathCtxt);
if (!resultPtr) {
throw new Error(`XPath evaluation failed: ${xpath}`);
}
// Process XPath result
// This would need proper xmlXPathObject struct parsing
const results = [];
this.libxml._xmlXPathFreeObject(resultPtr);
return results;
} finally {
this.libxml._xmlXPathFreeContext(xpathCtxt);
}
}
/**
* Serialize document to string
* @param {Object} options - Serialization options
* @returns {string} XML string
*/
toString(options = {}) {
try {
const encoding = options.encoding || 'UTF-8';
const format = options.format ? 1 : 0;
const bufferPtr = this.libxml._xmlSaveToBuffer(this.docPtr, encoding, format);
if (!bufferPtr) {
throw new Error('Failed to serialize document');
}
// Extract string from buffer
// This would need proper xmlBuffer handling
const xmlString = this.libxml.Module.UTF8ToString(bufferPtr);
this.libxml._xmlFree(bufferPtr);
return xmlString;
} catch (error) {
throw new Error(`Document serialization failed: ${error.message}`);
}
}
/**
* Clean up document resources
* @private
*/
_cleanup() {
if (this.docPtr) {
this.libxml._xmlFreeDoc(this.docPtr);
this.docPtr = null;
}
}
}
/**
* XML Element wrapper class
*/
class XMLElement {
constructor(libxml, nodePtr) {
this.libxml = libxml;
this.nodePtr = nodePtr;
}
/**
* Get attribute value
* @param {string} name - Attribute name
* @returns {string|null} Attribute value
*/
getAttribute(name) {
return this.libxml._xmlGetProp(this.nodePtr, name);
}
/**
* Set attribute value
* @param {string} name - Attribute name
* @param {string} value - Attribute value
*/
setAttribute(name, value) {
this.libxml._xmlSetProp(this.nodePtr, name, value);
}
/**
* Get element text content
* @returns {string} Text content
*/
getText() {
return this.libxml._xmlNodeGetContent(this.nodePtr);
}
/**
* Set element text content
* @param {string} content - Text content
*/
setText(content) {
this.libxml._xmlNodeSetContent(this.nodePtr, content);
}
}
// Factory function for creating libxml2 instance
async function createLibXML2(wasmModule) {
const libxml2 = new LibXML2WASM(wasmModule);
await libxml2.initialize();
return libxml2;
}
// Export for different module systems
if (typeof module !== 'undefined' && module.exports) {
// Node.js
module.exports = { LibXML2WASM, createLibXML2 };
} else if (typeof window !== 'undefined') {
// Browser
window.LibXML2WASM = LibXML2WASM;
window.createLibXML2 = createLibXML2;
}
EOF
print_success "JavaScript wrapper created"
}
# Create TypeScript definitions
create_typescript_definitions() {
print_status "Creating TypeScript definitions..."
cat > ${DIST_DIR}/libxml2-wasm.d.ts << 'EOF'
/**
* libxml2.wasm TypeScript Definitions
* High-performance XML processing library for WebAssembly
*/
export interface LibXML2Options {
loadExtDtd?: boolean;
substituteEntities?: boolean;
}
export interface ParserOptions {
encoding?: string;
recover?: boolean;
nonet?: boolean;
noerror?: boolean;
nowarning?: boolean;
}
export interface ValidationResult {
valid: boolean;
errors: ValidationError[];
}
export interface ValidationError {
message: string;
line: number;
column: number;
}
export interface SerializationOptions {
encoding?: string;
format?: boolean;
prettyPrint?: boolean;
}
export interface PerformanceStats {
documentsProcessed: number;
totalParseTime: number;
validationTime: number;
avgParseTime: number;
avgValidationTime: number;
}
export class XMLElement {
constructor(libxml: LibXML2WASM, nodePtr: number);
getAttribute(name: string): string | null;
setAttribute(name: string, value: string): void;
getText(): string;
setText(content: string): void;
}
export class XMLDocument {
constructor(libxml: LibXML2WASM, docPtr: number, docId: number);
getRoot(): XMLElement;
xpath(xpath: string): XMLElement[];
toString(options?: SerializationOptions): string;
}
export class LibXML2WASM {
constructor(wasmModule: any);
initialize(options?: LibXML2Options): Promise<boolean>;
parseString(xmlString: string, options?: ParserOptions): XMLDocument;
parseFile(filename: string, options?: ParserOptions): XMLDocument;
validateDocument(doc: XMLDocument, schema: XMLDocument): ValidationResult;
getStats(): PerformanceStats;
cleanup(): void;
}
export function createLibXML2(wasmModule: any): Promise<LibXML2WASM>;
export default LibXML2WASM;
EOF
print_success "TypeScript definitions created"
}
# Create WASM module with Emscripten
create_wasm_module() {
print_status "Creating WASM module with Emscripten..."
# Set up dependency paths
DEPS_INCLUDES=""
DEPS_LIBS=""
if [ "$USE_ZLIB" = "ON" ]; then
DEPS_INCLUDES="$DEPS_INCLUDES -I../zlib.wasm/install/include"
DEPS_LIBS="$DEPS_LIBS ../zlib.wasm/install/lib/libz.a"
fi
if [ "$USE_ICU" = "ON" ]; then
DEPS_INCLUDES="$DEPS_INCLUDES -I../icu.wasm/install/include"
DEPS_LIBS="$DEPS_LIBS ../icu.wasm/install/lib/libicu*.a"
fi
# Base Emscripten flags for optimized XML processing
BASE_FLAGS=(
-s WASM=1
-s MODULARIZE=1
-s EXPORT_ES6=1
-s EXPORT_NAME="'LibXML2Module'"
# Memory configuration for XML processing
-s INITIAL_MEMORY=${INITIAL_MEMORY}
-s MAXIMUM_MEMORY=${MAXIMUM_MEMORY}
-s ALLOW_MEMORY_GROWTH=1
-s STACK_SIZE=2MB
# Performance optimization
-O3
-flto
--closure 1
# Function exports
-s EXPORTED_FUNCTIONS="[
'_xmlInitParser',
'_xmlParseMemory',
'_xmlParseFile',
'_xmlFreeDoc',
'_xmlDocGetRootElement',
'_xmlGetProp',
'_xmlSetProp',
'_xmlNodeGetContent',
'_xmlNodeSetContent',
'_xmlXPathNewContext',
'_xmlXPathFreeContext',
'_xmlXPathEvalExpression',
'_xmlXPathFreeObject',
'_xmlSchemaNewDocParserCtxt',
'_xmlSchemaParse',
'_xmlSchemaNewValidCtxt',
'_xmlSchemaValidateDoc',
'_xmlSaveToBuffer',
'_xmlGetLastError',
'_xmlFree',
'_xmlMalloc',
'_xmlCleanupParser',
'_malloc',
'_free'
]"
-s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap', 'HEAPU8', 'UTF8ToString', 'writeStringToMemory']"
# File system (for Node.js environments)
-s FILESYSTEM=1
-s FORCE_FILESYSTEM=1
-s ENVIRONMENT=web,webview,worker,node
# Error handling
-s ASSERTIONS=1
-s DISABLE_EXCEPTION_CATCHING=0
# Function pointers for callbacks
-s RESERVED_FUNCTION_POINTERS=50
)
# Compile WASM module
emcc "${BASE_FLAGS[@]}" \
$DEPS_INCLUDES \
-I${INSTALL_DIR}/include \
-I${INSTALL_DIR}/include/libxml2 \
${INSTALL_DIR}/lib/libxml2.a \
$DEPS_LIBS \
-o ${DIST_DIR}/libxml2.js
print_success "WASM module created successfully"
# Create size report
print_status "Generating size report..."
echo "=== libxml2.wasm Build Size Report ===" > ${DIST_DIR}/size-report.txt
echo "Generated on: $(date)" >> ${DIST_DIR}/size-report.txt
echo "" >> ${DIST_DIR}/size-report.txt
if [ -f "${DIST_DIR}/libxml2.wasm" ]; then
WASM_SIZE=$(stat -f%z "${DIST_DIR}/libxml2.wasm" 2>/dev/null || stat -c%s "${DIST_DIR}/libxml2.wasm")
echo "WASM binary: ${WASM_SIZE} bytes" >> ${DIST_DIR}/size-report.txt
fi
if [ -f "${DIST_DIR}/libxml2.js" ]; then
JS_SIZE=$(stat -f%z "${DIST_DIR}/libxml2.js" 2>/dev/null || stat -c%s "${DIST_DIR}/libxml2.js")
echo "JavaScript: ${JS_SIZE} bytes" >> ${DIST_DIR}/size-report.txt
fi
echo "" >> ${DIST_DIR}/size-report.txt
echo "Dependencies used:" >> ${DIST_DIR}/size-report.txt
echo "- zlib: $USE_ZLIB" >> ${DIST_DIR}/size-report.txt
echo "- ICU: $USE_ICU" >> ${DIST_DIR}/size-report.txt
cat ${DIST_DIR}/size-report.txt
}
# Create package.json
create_package_json() {
print_status "Creating package.json..."
cat > package.json << 'EOF'
{
"name": "libxml2-wasm",
"version": "2.13.5-wasm.1",
"description": "High-performance XML processing library compiled to WebAssembly with comprehensive XML parsing, validation, and transformation capabilities",
"main": "dist/libxml2-wasm.js",
"module": "dist/libxml2-wasm.js",
"types": "dist/libxml2-wasm.d.ts",
"files": [
"dist/",
"README-WASM.md",
"CHANGELOG.md"
],
"scripts": {
"build": "./build-wasm.sh",
"test": "node test/test-libxml2-wasm.js",
"test:performance": "node test/performance-test.js",
"test:all": "npm test && npm run test:performance",
"benchmark": "node test/benchmark-suite.js",
"clean": "rm -rf build-wasm dist install",
"size-analysis": "node test/analyze-size.js"
},
"keywords": [
"xml", "wasm", "webassembly", "parser", "validation",
"xpath", "schema", "libxml2", "performance", "high-performance"
],
"author": "Superstruct Ltd, New Zealand",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/superstruct/libxml2.wasm"
},
"bugs": {
"url": "https://github.com/superstruct/libxml2.wasm/issues"
},
"homepage": "https://github.com/superstruct/libxml2.wasm#readme",
"engines": {
"node": ">=14.0.0"
},
"browser": {
"fs": false,
"path": false,
"os": false
},
"peerDependencies": {
"zlib-wasm": "^1.0.0"
},
"peerDependenciesMeta": {
"zlib-wasm": {
"optional": true
}
},
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0"
}
}
EOF
print_success "Package.json created"
}
# Main build function
main() {
print_status "Starting libxml2.wasm production build..."
echo "========================================"
check_dependencies
clean_build
configure_cmake
build_library
create_js_wrapper
create_typescript_definitions
create_wasm_module
create_package_json
print_success "Build completed successfully!"
echo ""
echo "Build artifacts:"
echo "- dist/libxml2.js - WASM module loader"
echo "- dist/libxml2.wasm - WebAssembly binary"
echo "- dist/libxml2-wasm.js - JavaScript API"
echo "- dist/libxml2-wasm.d.ts - TypeScript definitions"
echo "- package.json - NPM package configuration"
echo ""
print_status "Ready for testing and deployment!"
}
# Run main function
main "$@"