{"id":13438257,"url":"https://github.com/jameysharp/corrode","last_synced_at":"2025-05-15T18:02:15.042Z","repository":{"id":48853429,"uuid":"58161011","full_name":"jameysharp/corrode","owner":"jameysharp","description":"C to Rust translator","archived":false,"fork":false,"pushed_at":"2019-03-10T01:48:47.000Z","size":725,"stargazers_count":2167,"open_issues_count":79,"forks_count":114,"subscribers_count":67,"default_branch":"master","last_synced_at":"2025-03-31T22:16:12.814Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jameysharp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-05T21:12:52.000Z","updated_at":"2025-03-25T18:32:26.000Z","dependencies_parsed_at":"2022-08-31T13:21:10.091Z","dependency_job_id":null,"html_url":"https://github.com/jameysharp/corrode","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameysharp%2Fcorrode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameysharp%2Fcorrode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameysharp%2Fcorrode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameysharp%2Fcorrode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jameysharp","download_url":"https://codeload.github.com/jameysharp/corrode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744333,"owners_count":20988783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:04.040Z","updated_at":"2025-04-07T23:05:44.824Z","avatar_url":"https://github.com/jameysharp.png","language":"Haskell","funding_links":[],"categories":["Development tools","Haskell","开发工具 Development tools","Rust","开发工具","Tools"],"sub_categories":["Transpiling","转化 Transpiling","Other dialects and variants","跨语言调用 Transpiling"],"readme":"# Corrode: Automatic semantics-preserving translation from C to Rust\n\n[![Build Status](https://travis-ci.org/jameysharp/corrode.svg?branch=master)](https://travis-ci.org/jameysharp/corrode)\n\nThis program reads a C source file and prints an equivalent module in\nRust syntax. It's intended for partial automation of migrating legacy\ncode that was implemented in C. This tool does not fully automate the\njob because its output is only as safe as the input was; you should\nclean up the output afterward to use Rust features and idioms where\nappropriate.\n\n## Quick Start\n\nAs of now, there are no pre-built binaries available, so you need to build the\nproject yourself, but don't let that scare you away; clone the project, `cd`\ninto it and follow along :)\n\n### Windows\n\nIf you're using Windows, start by running `fixGitSymlinksForWindows.bat`\nas admin (this is necessary for Git to create symlinks).\n\n### Cabal\n\nEnsure that you have GHC and the `cabal-install` tool installed by following\nthe [directions on haskell.org](https://www.haskell.org/downloads#minimal).\nYou'll also need to have the `happy` and `alex` tools available in order to\nbuild `corrode`: you can install them with the `cabal-install` tool, as well.\nOnce you have installed the `cabal-install` tool, you can build `corrode` by\nnavigating to the `corrode` directory, installing the `happy` and `alex` tools,\nand then building and installing the `corrode` package:\n\n```\ncabal install happy\ncabal install alex\ncabal install\n```\n\nThis puts the `corrode` executable in `~/.cabal/bin`, so ensure that that\nlocation is in your `$PATH`.\n\n### Alternate instructions: Stack\n\nAlternately, you can use the [Haskell Stack](http://haskellstack.org) tool\nfor Haskell development. If you don't have it, head over to their website\nand follow the instructions for installing it on your machine.\n\nInstall the Glasgow Haskell Compiler using ```stack setup```. You can skip this\nstep if you already have a version of GHC installed on your system.\nYou can then build and install `corrode` by navigating to the `corrode`\ndirectory and running:\n\n```\nstack install\n```\n\nStack will build and install `corrode` to `~/.local/bin`. For ease of use, make\nsure that directory is in your `$PATH`.\n\nTo experiment with the project itself, you can build it using\n\n```\nstack build\n```\n\nthen run the executable:\n\n```bash\nstack exec -- corrode -Wall filename.c -I/usr/local/include -lm\n```\n\n## Usage\n\nThere are two ways to use Corrode. You can simply generate a `.rs` file\nfrom the C source, or you can do this _and_ compile in one step.\n\n### Generating Rust source\n\nYou can now run `corrode`, giving it any options that `gcc` would\naccept.\n\n```\ncorrode -Wall filename.c -I/usr/local/include -lm\n```\n\nIt will only use the options that are relevant to the C pre-processor,\nlike `-I` or `-D`, but since it accepts and ignores any other options,\nyou can usually get going just by changing `gcc` to `corrode` in the\n`gcc` invocation you've been using.\n\nUnlike a real C compiler, Corrode does not produce an object file or\nexecutable! Instead, if you ask it to process `filename.c`, it generates\nequivalent Rust source code in `filename.rs`. If you _do_ want object\ncode, there is a script to help with that.\n\n### Codegen with compilation\n\nYou can either invoke `rustc` on Corrode's output yourself (or import it\ninto a Rust project), or use the `scripts/corrode-cc` tool in place of\n`gcc` to compile and link. In many build systems, such as `make`, you\ncan simply set `CC=corrode-cc` without modification.\n\n## Design principles\n\nThe overarching goal of Corrode is to preserve the original properties\nof the source program as much as possible: behavior, ABI compatibility,\nand maintainability. We expect the output of Corrode to be used to\nreplace the original C, not just as an intermediate step in a compiler\ntoolchain.\n\nCorrode aims to produce Rust source code which behaves exactly the same\nway that the original C source behaved, if the input is free of\nundefined and implementation-defined behavior. In the presence of\nundefined behavior, we've tried to pick a behavior that isn't too\nsurprising. For example, if a signed addition might overflow (which is\nundefined behavior in C), Corrode just translates it to Rust's `+`\noperator, which panics on overflow in debug builds.\n\nThe compiled Rust source in turn will be ABI-compatible with the\noriginal C. If you compile Corrode-generated Rust to a `.o` file, you\ncan link to it exactly as if it were generated from the original C.\nEvery function that Corrode generates with be annotated with the `extern\n\"C\"` modifier.\n\nAt the same time, Corrode should produce code which is recognizably\nstructured like the original, so that the output is as maintainable as\nthe original. Every statement and every expression should be represented\nin the output\u0026mdash;in the same order, where possible. If a programmer\nwent to the trouble to put something in, we usually want it in the\ntranslated output; if it's not necessary, we can let the Rust compiler\nwarn about it.\n\nIf either behavior or ABI is not preserved, we consider that a bug in\nCorrode. However, it is not always possible to preserve the structure of\nthe original code, so we do the best that we can.\n\n## Testing\n\nSo far, Corrode has primarily been tested by generating random C\nprograms using [csmith](https://github.com/csmith-project/csmith),\nfixing Corrode until it can handle all syntax used in that particular\nprogram, and verifying that the resulting Rust module compiles without\nerrors.\n\nVerifying that the translated output is equivalent to the input is not\ntrivial. One approach I think is worth trying is to use the\nGalois [Software Analysis Workbench](http://saw.galois.com/) to prove\nthat the LLVM bitcode generated from `clang` on a C source file is\nequivalent to the LLVM bitcode generated from `rustc` on a Rust source\nfile from Corrode. SAW uses a symbolic simulator over LLVM bitcode to\nextract logical formulas representing the behavior of each function, and\nthen uses an SMT solver to prove equivalence between pairs of formulas.\nGenerating large numbers of random C programs using csmith and then\nproving the translation results equivalent for each one should give\npretty good confidence in the implementation.\n\nBecause the project is still in its early phases, it is not yet possible\nto translate most real C programs or libraries. But if you have one you\nparticularly want to try out, I'd love to get pull requests implementing\nmore of C!\n\n## Contributing\n\nIf this seems cool and you'd like to help complete it, welcome! There\nare quite a few fundamental pieces of the C standard which are not yet\nimplemented. I'd love to chat with you if you're not quite sure how to\nget started! You can e-mail me at \u003cmailto:jamey@minilop.net\u003e.\n\n## What Corrode is not\n\nA Rust module that exactly captures the semantics of a C source file is\na Rust module that doesn't look very much like Rust. ;-) I would like to\nbuild a companion tool which rewrites parts of a valid Rust program in\nways that have the same result but make use of Rust idioms. I think it\nshould be separate from this tool because I expect it to be useful for\nother folks, not just users of Corrode. I propose to call that program\n\"idiomatic\", and I think it should be written in Rust using the Rust AST\nfrom [`syntex_syntax`](https://github.com/serde-rs/syntex).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameysharp%2Fcorrode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjameysharp%2Fcorrode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameysharp%2Fcorrode/lists"}