summaryrefslogtreecommitdiff
path: root/Mk/Scripts/cargo-crates-git-configure.awk
blob: bb2884479d226cd9e829cb802df4fed1253f56fb (plain) (blame)
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
# MAINTAINER: rust@FreeBSD.org

function parse_sources(		sources_len, raw_sources, i, j, k, original_crate_source, url, crate_source, crate_names, crate_name) {
	sources_len = 0
	split(GIT_SOURCES, raw_sources)
	for (i = 1; i <= length(raw_sources); i++) {
		j = index(raw_sources[i], "@")
		if (j == 0) {
			warn("invalid source: %s\n", raw_sources[i])
			continue
		}
		original_crate_source = substr(raw_sources[i], j + 1)
		split_url(url, original_crate_source)
		sub(/^git\+/, "", url["scheme"])
		delete url["fragment"]
		delete url["query"]
		#sub(/\.git$/, "", url["path"])
		crate_source = join_url(url)

		split(substr(raw_sources[i], 1, j - 1), crate_names, ",")
		for (k = 1; k <= length(crate_names); k++) {
			crate_name = crate_names[k]
			if (!source_crates[crate_source]) {
				sources[++sources_len] = crate_source
			}
			source_crates[crate_source] = source_crates[crate_source] " " crate_name
			original_crate_sources[crate_source, crate_name] = original_crate_source
		}
	}
}

function get_source_dir(crate_source, crate_name,		git_info, path, in_package, pattern, cmd, cargotoml, line) {
	if (!split_git_url(git_info, original_crate_sources[crate_source, crate_name])) {
		exit 1
	}
	path = WRKDIR "/" git_info["dir"]
	# Try to find the first Cargo.toml that defines our crate
	# We are looking for
	# [package]
	# name = "$crate_name"
	in_package = 0
	pattern = sprintf("^[ \t]*name[ \t]*=[ \t]*['\"]%s['\"]", crate_name)
	cmd = FIND " " path " -name Cargo.toml -type f"
	while ((cmd | getline cargotoml) > 0) {
		while (getline line <cargotoml) {
			if (in_package && line ~ pattern) {
				path = cargotoml
				sub(/\/Cargo\.toml$/, "", path)
				close(cmd)
				close(cargotoml)
				return path
			} else if (line ~ /^[ \t]*\[[ \t]*package[ \t]*\][ \t]*$/) {
				in_package = 1
			} else if (line ~ /^[ \t]*\[/) {
				in_package = 0
			}
		}
		close(cargotoml)
	}
	close(cmd)

	return path
}

function find_replaced_crates(input, output,		in_patch_crates_io, line, cols) {
	delete replaced_crates
# When Cargo.toml has constructs like this (e.g., www/miniserve, x11/wezterm )
# [patch.crates-io]
# mime_guess = { git = "https://github.com/svenstaro/mime_guess.git" }
# cargo fails to find the replacements we setup.  Check for this
# and replace with our own [patch.crates-io] section.
# Note that we need to filter out the original patch section.
	in_patch_crates_io = 0
	while (getline line <input) {
		if (in_patch_crates_io) {
			if (line ~ /[ \t]*git[ \t]*=/ && line !~ /^[ \t]*#/) {
				split(line, cols)
				replaced_crates[cols[1]] = 1
				print "# " line >output
				continue
			}
		} else if (line ~ /^[ \t]*\[[ \t]*patch\.crates-io[ \t]*\][ \t]*$/) {
			in_patch_crates_io = 1
		} else if (line ~ /^[ \t]*\[/) {
			in_patch_crates_io = 0
		}
		print line >output
	}
	close(input)
	close(output)
}

function add_crates_io_patches(		print_header, local_crates, cmd, cargotoml, source, crates) {
	print_header = 0
# --exclude-dir not supported on FreeBSD < 13
#	cmd = GREP " --include='*/Cargo.toml' --exclude-dir='" CARGO_VENDOR_DIR "' -Flr 'patch.crates-io' " WRKSRC
	cmd = FIND " " WRKSRC " -name Cargo.toml -not -path '" CARGO_VENDOR_DIR "/*' -exec " GREP " -Flr 'patch.crates-io' {} \\\+"
	while (cmd | getline cargotoml) {
		if (0 != system(CP " " cargotoml " " cargotoml ".orig-cargo")) {
			exit 1
		}
		find_replaced_crates(cargotoml ".orig-cargo", cargotoml)
		if (length(replaced_crates) > 0) {
			for (i in sources) {
				source = sources[i]
				split(source_crates[source], crates)
				for (j in crates) {
					if (replaced_crates[crates[j]]) {
						print_header = 1
						local_crates[crates[j]] = get_source_dir(source, crates[j])
					}
				}
			}
		}
	}
	if (print_header == 1) {
		printf("[patch.crates-io]\n")

		for (i in local_crates) {
			printf("%s = { path = '%s' }\n", i, local_crates[i])
		}
	}
	close(cmd)
}

function add_git_patches(		i, j, source, crates) {
	for (i = 1; i <= length(sources); i++) {
		source = sources[i]
		split(source_crates[source], crates)
		printf("[patch.'%s']\n", source)
		for (j = 1; j <= length(crates); j++) {
			printf("%s = { path = '%s' }\n", crates[j], get_source_dir(source, crates[j]))
		}
	}
}

END {
	parse_sources()
	add_crates_io_patches()
	add_git_patches()
}