NAIROBI, Kenya >> Lawmakers in Uganda have passed a sweeping anti-gay law that can bring punishments as severe as the death penalty — the culmination of a long-running campaign to criminalize homosexuality and target LGBTQ people in the conservative nation in East Africa.

The law, which was passed late Tuesday night after more than seven hours of discussion and amendments, calls for a life sentence for anyone engaging in gay sex. Even trying to have same-sex relations would be met with a seven-year prison term.

The death penalty would be applied to people convicted of “aggravated homosexuality,” a sweeping term defined in the law as homosexual acts committed by anyone infected with HIV or involving children, disabled people or anyone drugged against their will. Most of these acts are already crimes regardless of gender under the Ugandan penal code, but the death penalty has been added to the bill to target cases in which the perpetrator and victim are of the same sex.

The parliamentary vote caps a struggle over gay rights in Uganda that has drawn international attention for nearly 15 years. It comes as anti-gay policies and discrimination have been on the rise in several African nations, including Kenya, Ghana and Zambia.

The legislation in Uganda, called the Anti-Homosexuality Bill, also imposes a penalty of up to 1 billion Ugandan shillings (about $264,000) on any entity convicted of promoting homosexuality. People younger than 18 who are convicted of engaging in homosexuality face up to three years in prison, along with a period of “rehabilitation.”

“This house will continue to pass laws that recognize, protect and safeguard the sovereignty, morals and cultures of this country,” Anita Annet Among, speaker of the Ugandan Parliament, said after legislators finished voting.

The bill will now go to President Yoweri Museveni, Uganda’s leader for nearly four decades, who has been an outspoken driver of anti-gay measures. He has in the past accused gay people of undermining the stability of Uganda and in recent weeks called them “deviants.”

Museveni is also a close Western ally whose nation receives almost $1 billion a year in development aid from the United States. He has pressed for anti-gay measures despite exhortations by Western nations to respect the rights of LGBTQ citizens and in defiance of threats to cut aid.

On Wednesday, Secretary of State Antony Blinken urged the Ugandan government “to strongly reconsider the implementation of this legislation,” saying that it would undermine the rights of Ugandans and “could reverse gains” in the fight against HIV and AIDS.

The bill’s passing was sharply criticized by rights groups and by a few lawmakers in Uganda who said that it infringed on the freedoms of Ugandans and further eroded the rights of gay people.

Volker Turk, the United Nations human rights chief, called the anti-gay law “probably among the worst of its kind in the world” and said that it could “serve to incite people against each other.”

Homosexuality is illegal in at least three dozen African countries, with sentences ranging from fines to life in prison. Around the world, a death penalty sentence for same-sex relations is imposed in only a handful of countries, including Iran and Mauritania, according to a survey by Human Rights Watch.

The bill was introduced in early March by lawmaker Asuman Basalirwa, who has said that homosexuality threatens family values and the safety of Ugandan children. Basalirwa did not respond to an interview request.