svy-io 0.0.2a0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. svy_io-0.0.2a0/LICENSE.md +21 -0
  2. svy_io-0.0.2a0/PKG-INFO +129 -0
  3. svy_io-0.0.2a0/README.md +115 -0
  4. svy_io-0.0.2a0/native/readstat-sys/Cargo.toml +22 -0
  5. svy_io-0.0.2a0/native/readstat-sys/build.rs +293 -0
  6. svy_io-0.0.2a0/native/readstat-sys/src/lib.rs +3 -0
  7. svy_io-0.0.2a0/native/readstat-sys/wrapper.h +2 -0
  8. svy_io-0.0.2a0/native/svyreadstat_rs/Cargo.lock +1564 -0
  9. svy_io-0.0.2a0/native/svyreadstat_rs/Cargo.toml +44 -0
  10. svy_io-0.0.2a0/native/svyreadstat_rs/__init__.py +0 -0
  11. svy_io-0.0.2a0/native/svyreadstat_rs/benches/parse_benchmarks.rs +81 -0
  12. svy_io-0.0.2a0/native/svyreadstat_rs/src/core copy.rs +588 -0
  13. svy_io-0.0.2a0/native/svyreadstat_rs/src/core.rs +600 -0
  14. svy_io-0.0.2a0/native/svyreadstat_rs/src/lib.rs +32 -0
  15. svy_io-0.0.2a0/native/svyreadstat_rs/src/sas_read copy.rs +111 -0
  16. svy_io-0.0.2a0/native/svyreadstat_rs/src/sas_read.rs +206 -0
  17. svy_io-0.0.2a0/native/svyreadstat_rs/src/spss_read copy.rs +161 -0
  18. svy_io-0.0.2a0/native/svyreadstat_rs/src/spss_read.rs +203 -0
  19. svy_io-0.0.2a0/native/svyreadstat_rs/src/spss_write.rs +662 -0
  20. svy_io-0.0.2a0/native/svyreadstat_rs/src/stata_read copy.rs +92 -0
  21. svy_io-0.0.2a0/native/svyreadstat_rs/src/stata_read.rs +98 -0
  22. svy_io-0.0.2a0/native/svyreadstat_rs/src/stata_write copy.rs +443 -0
  23. svy_io-0.0.2a0/native/svyreadstat_rs/src/stata_write.rs +443 -0
  24. svy_io-0.0.2a0/native/svyreadstat_rs/src/xpt_read copy.rs +88 -0
  25. svy_io-0.0.2a0/native/svyreadstat_rs/src/xpt_read.rs +88 -0
  26. svy_io-0.0.2a0/native/svyreadstat_rs/src/xpt_write.rs +340 -0
  27. svy_io-0.0.2a0/pyproject.toml +80 -0
  28. svy_io-0.0.2a0/python/svy_io/__init__.py +74 -0
  29. svy_io-0.0.2a0/python/svy_io/factor.py +71 -0
  30. svy_io-0.0.2a0/python/svy_io/helpers.py +75 -0
  31. svy_io-0.0.2a0/python/svy_io/labelled.py +615 -0
  32. svy_io-0.0.2a0/python/svy_io/metadata.py +35 -0
  33. svy_io-0.0.2a0/python/svy_io/sas.py +841 -0
  34. svy_io-0.0.2a0/python/svy_io/spss.py +532 -0
  35. svy_io-0.0.2a0/python/svy_io/stata.py +544 -0
  36. svy_io-0.0.2a0/python/svy_io/tagged_na.py +103 -0
  37. svy_io-0.0.2a0/python/svy_io/temporals.py +297 -0
  38. svy_io-0.0.2a0/python/svy_io/utils.py +110 -0
  39. svy_io-0.0.2a0/python/svy_io/zap.py +458 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Samplics LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.4
2
+ Name: svy-io
3
+ Version: 0.0.2a0
4
+ Classifier: License :: OSI Approved :: MIT License
5
+ Requires-Dist: polars>=1.34.0
6
+ Requires-Dist: pyarrow>=21
7
+ License-File: LICENSE.md
8
+ Summary: SAS/SPSS/Stata I/O with Polars, powered by ReadStat + PyO3
9
+ Home-Page: https://svylab.com/docs/svy-io
10
+ Author: Samplics LLC
11
+ Requires-Python: >=3.11
12
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
13
+
14
+ # svy-io
15
+
16
+ Lightweight, Pythonic file I/O for **SAS**, **SPSS**, and **Stata** powered by the ReadStat C library. Returns **Polars** DataFrames and preserves useful metadata — with a tiny, consistent API.
17
+
18
+ ---
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install svy-io
24
+ ```
25
+
26
+ **Requires:** Python, [Polars](https://pola-rs.github.io/polars/), and [pyarrow](https://arrow.apache.org/docs/python/) (installed automatically as a dependency).
27
+
28
+ ---
29
+
30
+ ## What you get
31
+
32
+ - **SAS**: read `.sas7bdat` and `.xpt`; write `.xpt`
33
+ - **SPSS**: read `.sav`, `.zsav`, `.por`; write `.sav` (incl. `.zsav` via `compress="zsav"`)
34
+ - **Stata**: read & write `.dta` (v8–15)
35
+ - **Polars-first**: fast DataFrames; each `read_*` returns `(df, meta)`
36
+
37
+ ---
38
+
39
+ ## Usage
40
+
41
+ Below are the only things you need: `read_*` and `write_*`.
42
+
43
+ ### SAS
44
+
45
+ ```python
46
+ # Read
47
+ from svy_io.sas import read_sas, read_xpt
48
+
49
+ df, meta = read_sas("data.sas7bdat") # optional: catalog_path="formats.sas7bcat"
50
+ df_xpt, meta_xpt = read_xpt("transport.xpt")
51
+
52
+ # Write (XPT v8 recommended)
53
+ from svy_io.sas import write_xpt
54
+ import polars as pl
55
+
56
+ df_out = pl.DataFrame({"id": [1, 2, 3], "score": [10.5, 9.3, 8.8]})
57
+ write_xpt(df_out, "output.xpt", version=8, label="Study Data")
58
+ ```
59
+
60
+ ---
61
+
62
+ ### SPSS
63
+
64
+ ```python
65
+ # Read
66
+ from svy_io.spss import read_sav, read_por, read_spss
67
+
68
+ df_sav, meta_sav = read_sav("survey.sav") # .zsav handled automatically
69
+ df_por, meta_por = read_por("legacy.por")
70
+ df_auto, meta_auto = read_spss("data.sav") # auto by extension
71
+
72
+ # Write
73
+ from svy_io.spss import write_sav
74
+ import polars as pl
75
+
76
+ df = pl.DataFrame({"subject_id": [1, 2, 3], "age": [25, 30, 35], "gender": [1, 2, 1]})
77
+
78
+ write_sav(
79
+ df,
80
+ "out.sav", # or "out.zsav" with compress="zsav"
81
+ compress="byte",
82
+ var_labels={"age": "Age (years)", "gender": "Gender"},
83
+ value_labels=[{"col": "gender", "labels": {"1": "Male", "2": "Female"}}],
84
+ user_missing=[{"col": "age", "values": [-99]}]
85
+ )
86
+ ```
87
+
88
+ ---
89
+
90
+ ### Stata
91
+
92
+ ```python
93
+ # Read
94
+ from svy_io.stata import read_dta
95
+
96
+ df, meta = read_dta("data.dta")
97
+
98
+ # Write
99
+ from svy_io.stata import write_dta
100
+ import polars as pl
101
+
102
+ df_out = pl.DataFrame({"id": [1, 2, 3], "income": [50000, 62000, 58000]})
103
+
104
+ write_dta(
105
+ df_out,
106
+ "output.dta",
107
+ version=15, # 8–15 supported
108
+ file_label="Survey Data 2024",
109
+ var_labels={"income": "Annual income (USD)"}
110
+ )
111
+ ```
112
+
113
+ ---
114
+
115
+ ## Notes & tips
116
+
117
+ - All readers return a tuple: `(df: polars.DataFrame, meta: dict)`.
118
+ - If you work with dates, many `read_*` functions support `coerce_temporals=True`.
119
+ - For large files, consider `cols_skip=[...]` and/or `n_max=...` while exploring.
120
+ - Stata limits strings to 2045 bytes; SAS XPT v5 has stricter name/length limits — prefer XPT v8 unless you need legacy compatibility.
121
+
122
+ ---
123
+
124
+ ## Help & links
125
+
126
+ - Source & issues: https://github.com/samplics-org/svy-io
127
+ - ReadStat (upstream): https://github.com/WizardMac/ReadStat
128
+ - Polars docs: https://pola-rs.github.io/polars/
129
+
@@ -0,0 +1,115 @@
1
+ # svy-io
2
+
3
+ Lightweight, Pythonic file I/O for **SAS**, **SPSS**, and **Stata** powered by the ReadStat C library. Returns **Polars** DataFrames and preserves useful metadata — with a tiny, consistent API.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install svy-io
11
+ ```
12
+
13
+ **Requires:** Python, [Polars](https://pola-rs.github.io/polars/), and [pyarrow](https://arrow.apache.org/docs/python/) (installed automatically as a dependency).
14
+
15
+ ---
16
+
17
+ ## What you get
18
+
19
+ - **SAS**: read `.sas7bdat` and `.xpt`; write `.xpt`
20
+ - **SPSS**: read `.sav`, `.zsav`, `.por`; write `.sav` (incl. `.zsav` via `compress="zsav"`)
21
+ - **Stata**: read & write `.dta` (v8–15)
22
+ - **Polars-first**: fast DataFrames; each `read_*` returns `(df, meta)`
23
+
24
+ ---
25
+
26
+ ## Usage
27
+
28
+ Below are the only things you need: `read_*` and `write_*`.
29
+
30
+ ### SAS
31
+
32
+ ```python
33
+ # Read
34
+ from svy_io.sas import read_sas, read_xpt
35
+
36
+ df, meta = read_sas("data.sas7bdat") # optional: catalog_path="formats.sas7bcat"
37
+ df_xpt, meta_xpt = read_xpt("transport.xpt")
38
+
39
+ # Write (XPT v8 recommended)
40
+ from svy_io.sas import write_xpt
41
+ import polars as pl
42
+
43
+ df_out = pl.DataFrame({"id": [1, 2, 3], "score": [10.5, 9.3, 8.8]})
44
+ write_xpt(df_out, "output.xpt", version=8, label="Study Data")
45
+ ```
46
+
47
+ ---
48
+
49
+ ### SPSS
50
+
51
+ ```python
52
+ # Read
53
+ from svy_io.spss import read_sav, read_por, read_spss
54
+
55
+ df_sav, meta_sav = read_sav("survey.sav") # .zsav handled automatically
56
+ df_por, meta_por = read_por("legacy.por")
57
+ df_auto, meta_auto = read_spss("data.sav") # auto by extension
58
+
59
+ # Write
60
+ from svy_io.spss import write_sav
61
+ import polars as pl
62
+
63
+ df = pl.DataFrame({"subject_id": [1, 2, 3], "age": [25, 30, 35], "gender": [1, 2, 1]})
64
+
65
+ write_sav(
66
+ df,
67
+ "out.sav", # or "out.zsav" with compress="zsav"
68
+ compress="byte",
69
+ var_labels={"age": "Age (years)", "gender": "Gender"},
70
+ value_labels=[{"col": "gender", "labels": {"1": "Male", "2": "Female"}}],
71
+ user_missing=[{"col": "age", "values": [-99]}]
72
+ )
73
+ ```
74
+
75
+ ---
76
+
77
+ ### Stata
78
+
79
+ ```python
80
+ # Read
81
+ from svy_io.stata import read_dta
82
+
83
+ df, meta = read_dta("data.dta")
84
+
85
+ # Write
86
+ from svy_io.stata import write_dta
87
+ import polars as pl
88
+
89
+ df_out = pl.DataFrame({"id": [1, 2, 3], "income": [50000, 62000, 58000]})
90
+
91
+ write_dta(
92
+ df_out,
93
+ "output.dta",
94
+ version=15, # 8–15 supported
95
+ file_label="Survey Data 2024",
96
+ var_labels={"income": "Annual income (USD)"}
97
+ )
98
+ ```
99
+
100
+ ---
101
+
102
+ ## Notes & tips
103
+
104
+ - All readers return a tuple: `(df: polars.DataFrame, meta: dict)`.
105
+ - If you work with dates, many `read_*` functions support `coerce_temporals=True`.
106
+ - For large files, consider `cols_skip=[...]` and/or `n_max=...` while exploring.
107
+ - Stata limits strings to 2045 bytes; SAS XPT v5 has stricter name/length limits — prefer XPT v8 unless you need legacy compatibility.
108
+
109
+ ---
110
+
111
+ ## Help & links
112
+
113
+ - Source & issues: https://github.com/samplics-org/svy-io
114
+ - ReadStat (upstream): https://github.com/WizardMac/ReadStat
115
+ - Polars docs: https://pola-rs.github.io/polars/
@@ -0,0 +1,22 @@
1
+ [package]
2
+ name = "readstat-sys"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ links = "readstat"
6
+ license = "MIT"
7
+ repository = "https://github.com/samplics-org/svy-io"
8
+ homepage = "https://svylab.com/docs/svy-io"
9
+ description = "ReadStat FFI (vendored or system)"
10
+
11
+ [build-dependencies]
12
+ bindgen = "0.70"
13
+ pkg-config = "0.3"
14
+ cc = "1"
15
+ walkdir = "2" # <- to glob C sources in vendored mode
16
+
17
+ [dependencies]
18
+ libc = "0.2"
19
+
20
+ [features]
21
+ # Default to vendored in CI; you can leave it off locally if you prefer system libs
22
+ vendored = []
@@ -0,0 +1,293 @@
1
+ use std::{
2
+ env, fs,
3
+ path::{Path, PathBuf},
4
+ };
5
+
6
+ fn bindgen_with_includes(include_dir: &Path) {
7
+ let builder = bindgen::Builder::default()
8
+ .header("wrapper.h")
9
+ .allowlist_function("readstat_.*")
10
+ .allowlist_type("readstat_.*")
11
+ .allowlist_var("READSTAT_.*")
12
+ .layout_tests(false)
13
+ .clang_arg(format!("-I{}", include_dir.display()));
14
+
15
+ let out = PathBuf::from(env::var("OUT_DIR").unwrap());
16
+ builder
17
+ .generate()
18
+ .expect("bindgen failed for readstat")
19
+ .write_to_file(out.join("bindings.rs"))
20
+ .expect("Couldn't write bindings!");
21
+ println!("cargo:rerun-if-changed=wrapper.h");
22
+ }
23
+
24
+ fn find_readstat_dir() -> Option<PathBuf> {
25
+ if let Some(p) = env::var_os("READSTAT_SRC") {
26
+ let p = PathBuf::from(p);
27
+ if p.join("src/readstat.h").exists() {
28
+ return Some(p);
29
+ }
30
+ }
31
+ let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
32
+ let vendored = crate_dir.join("third_party/readstat");
33
+ if vendored.join("src/readstat.h").exists() {
34
+ return Some(vendored);
35
+ }
36
+ if let Some(root) = crate_dir.parent().and_then(|p| p.parent()) {
37
+ let root_readstat = root.join("ReadStat");
38
+ if root_readstat.join("src/readstat.h").exists() {
39
+ return Some(root_readstat);
40
+ }
41
+ }
42
+ None
43
+ }
44
+
45
+ fn build_vendored(rs_dir: &Path) {
46
+ let src_dir = rs_dir.join("src");
47
+ let inc_dir = rs_dir.join("src");
48
+ assert!(
49
+ src_dir.exists(),
50
+ "ReadStat sources not found at {}",
51
+ src_dir.display()
52
+ );
53
+
54
+ let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
55
+
56
+ let mut build = cc::Build::new();
57
+
58
+ // Put OUT_DIR first so our stub headers win on Windows
59
+ build.include(&out_dir);
60
+ build.include(&inc_dir);
61
+ build.include(&rs_dir);
62
+
63
+ // Minimal config.h stand-ins
64
+ build.define("HAVE_STDDEF_H", Some("1"));
65
+ build.define("HAVE_STDINT_H", Some("1"));
66
+ build.define("HAVE_INTTYPES_H", Some("1"));
67
+ build.define("HAVE_STDLIB_H", Some("1"));
68
+ build.define("HAVE_STRING_H", Some("1"));
69
+ build.define("HAVE_STRINGS_H", Some("1"));
70
+
71
+ // zlib: off on Windows (we also skip zsav sources below)
72
+ let has_zlib = !cfg!(target_os = "windows");
73
+ if has_zlib {
74
+ build.define("READSTAT_HAVE_ZLIB", Some("1"));
75
+ build.define("HAVE_ZLIB", Some("1"));
76
+ println!("cargo:rustc-link-lib=z");
77
+ eprintln!("Building WITH zlib support");
78
+ } else {
79
+ build.define("READSTAT_HAVE_ZLIB", Some("0"));
80
+ build.define("HAVE_ZLIB", Some("0"));
81
+ eprintln!("WARNING: Building WITHOUT zlib support");
82
+ eprintln!("WARNING: .zsav (compressed SPSS) files will NOT be supported on Windows");
83
+ }
84
+
85
+ // iconv availability + stub implementations for Windows
86
+ if cfg!(target_os = "windows") {
87
+ build.define("HAVE_ICONV", Some("0"));
88
+
89
+ // Write a minimal iconv.h so readstat_iconv.h can #include it
90
+ let stub_h = out_dir.join("iconv.h");
91
+ fs::write(
92
+ &stub_h,
93
+ r#"
94
+ #ifndef ICONV_STUB_H
95
+ #define ICONV_STUB_H
96
+ #include <stddef.h>
97
+ typedef void* iconv_t;
98
+ #define ICONV_CONST const
99
+ #endif
100
+ "#,
101
+ )
102
+ .expect("write iconv.h stub");
103
+
104
+ // Create comprehensive stub implementations for Windows
105
+ let stub_c = out_dir.join("posix_stubs.c");
106
+ fs::write(
107
+ &stub_c,
108
+ r#"
109
+ #include <stddef.h>
110
+ #include <errno.h>
111
+
112
+ typedef void* iconv_t;
113
+
114
+ // iconv stubs (character encoding conversion)
115
+ iconv_t iconv_open(const char* tocode, const char* fromcode) {
116
+ (void)tocode;
117
+ (void)fromcode;
118
+ return (iconv_t)-1; // Return error
119
+ }
120
+
121
+ int iconv_close(iconv_t cd) {
122
+ (void)cd;
123
+ return 0;
124
+ }
125
+
126
+ size_t iconv(iconv_t cd, const char** inbuf, size_t* inbytesleft,
127
+ char** outbuf, size_t* outbytesleft) {
128
+ (void)cd;
129
+ (void)inbuf;
130
+ (void)inbytesleft;
131
+ (void)outbuf;
132
+ (void)outbytesleft;
133
+ errno = EINVAL;
134
+ return (size_t)-1; // Return error
135
+ }
136
+
137
+ // Forward declaration of the Windows I/O structure
138
+ typedef struct readstat_io_s readstat_io_t;
139
+
140
+ // Windows I/O initialization stub
141
+ // This should be defined in readstat_io_win.c, but we provide a fallback
142
+ readstat_io_t* unistd_io_init(void) {
143
+ return NULL; // Return NULL to indicate no I/O support
144
+ }
145
+ "#,
146
+ )
147
+ .expect("write posix_stubs.c");
148
+
149
+ // Add stub to build
150
+ build.file(&stub_c);
151
+ println!("cargo:rerun-if-changed={}", stub_c.display());
152
+ eprintln!("Created POSIX stubs for Windows");
153
+ } else {
154
+ build.define("HAVE_ICONV", Some("1"));
155
+ #[cfg(target_os = "macos")]
156
+ println!("cargo:rustc-link-lib=iconv");
157
+ }
158
+
159
+ // Force-include readstat.h for all TUs
160
+ if cfg!(target_env = "msvc") {
161
+ build.flag("/FIreadstat.h");
162
+ } else {
163
+ build.flag("-include").flag("readstat.h");
164
+ }
165
+
166
+ // Collect C files
167
+ let mut files: Vec<PathBuf> = Vec::new();
168
+ let mut skipped_files: Vec<String> = Vec::new();
169
+
170
+ for entry in walkdir::WalkDir::new(&src_dir) {
171
+ let entry = entry.unwrap();
172
+ let p = entry.path();
173
+ if !entry.file_type().is_file() {
174
+ continue;
175
+ }
176
+ if p.extension().and_then(|s| s.to_str()) != Some("c") {
177
+ continue;
178
+ }
179
+
180
+ let rel = p.strip_prefix(&src_dir).unwrap();
181
+
182
+ // Skip test/bin directories
183
+ let skip_dir = rel.components().any(|c| {
184
+ matches!(
185
+ c.as_os_str().to_str(),
186
+ Some("bin" | "fuzz" | "test" | "tests" | "txt")
187
+ )
188
+ });
189
+ if skip_dir {
190
+ continue;
191
+ }
192
+
193
+ let name = rel.file_name().and_then(|s| s.to_str()).unwrap_or("");
194
+
195
+ // Platform IO backend - IMPORTANT: Include readstat_io_win.c on Windows!
196
+ if cfg!(target_os = "windows") {
197
+ if name == "readstat_io_unistd.c" {
198
+ skipped_files.push(format!("{} (Unix I/O)", name));
199
+ continue;
200
+ }
201
+ // Make sure we include readstat_io_win.c
202
+ if name == "readstat_io_win.c" {
203
+ eprintln!("Including Windows I/O: {}", p.display());
204
+ }
205
+ } else {
206
+ if name == "readstat_io_win.c" {
207
+ skipped_files.push(format!("{} (Windows I/O)", name));
208
+ continue;
209
+ }
210
+ }
211
+
212
+ // CRITICAL: Skip zlib-dependent files when zlib is not available
213
+ if !has_zlib {
214
+ if name == "readstat_zsav_compress.c"
215
+ || name == "readstat_zsav_read.c"
216
+ || name == "readstat_zsav_write.c"
217
+ {
218
+ skipped_files.push(format!("{} (requires zlib)", name));
219
+ continue;
220
+ }
221
+ }
222
+
223
+ files.push(p.to_path_buf());
224
+ }
225
+
226
+ // Print what we're skipping for debugging
227
+ if !skipped_files.is_empty() {
228
+ eprintln!("Skipping {} files:", skipped_files.len());
229
+ for f in &skipped_files {
230
+ eprintln!(" - {}", f);
231
+ }
232
+ }
233
+
234
+ eprintln!("Compiling {} C source files", files.len());
235
+
236
+ // Add files to build
237
+ for f in &files {
238
+ println!("cargo:rerun-if-changed={}", f.display());
239
+ build.file(f);
240
+ }
241
+
242
+ build.define("READSTAT_VERSION", Some("\"vendored\""));
243
+ build.warnings(false).compile("readstat");
244
+
245
+ // Bindings
246
+ bindgen_with_includes(&inc_dir);
247
+ }
248
+
249
+ fn link_from_prefix(prefix: &str) {
250
+ println!("cargo:rustc-link-search=native={prefix}/lib");
251
+ println!("cargo:rustc-link-lib=readstat");
252
+ println!("cargo:rustc-link-lib=z");
253
+ println!("cargo:include={prefix}/include");
254
+ bindgen_with_includes(&PathBuf::from(format!("{prefix}/include")));
255
+ }
256
+
257
+ fn link_from_pkg_config() -> bool {
258
+ match pkg_config::Config::new().probe("readstat") {
259
+ Ok(lib) => {
260
+ if let Some(inc) = lib.include_paths.get(0) {
261
+ bindgen_with_includes(inc);
262
+ } else {
263
+ bindgen_with_includes(Path::new("."));
264
+ }
265
+ true
266
+ }
267
+ Err(_) => false,
268
+ }
269
+ }
270
+
271
+ fn main() {
272
+ if cfg!(feature = "vendored") {
273
+ if let Some(dir) = find_readstat_dir() {
274
+ build_vendored(&dir);
275
+ return;
276
+ }
277
+ panic!("`vendored` enabled but could not find ReadStat sources. Set READSTAT_SRC or add a submodule at native/readstat-sys/third_party/readstat or ./ReadStat");
278
+ }
279
+
280
+ if link_from_pkg_config() {
281
+ return;
282
+ }
283
+ if let Ok(prefix) = env::var("READSTAT_PREFIX") {
284
+ link_from_prefix(&prefix);
285
+ return;
286
+ }
287
+ if let Ok(home) = env::var("HOME") {
288
+ link_from_prefix(&format!("{home}/.local"));
289
+ return;
290
+ }
291
+
292
+ panic!("Unable to locate ReadStat: enable feature `vendored`, install via pkg-config, or set READSTAT_PREFIX.");
293
+ }
@@ -0,0 +1,3 @@
1
+ // native/readstat-sys/src/lib.rs
2
+ #![allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]
3
+ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
@@ -0,0 +1,2 @@
1
+ // native/readstat-sys/wrapper.h
2
+ #include <readstat.h>